C++ method to call external application exe

1. ShellExecute function

   function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,Directory: PChar; ShowCmd: Integer): HINST; stdcall;

 ● hWnd: used to specify the parent window handle. When an error occurs during the function call, it will act as the parent window of the Windows message window. For example, it can be set to the application's main window handle, Application.Handle, or it can be set to the desktop window handle (obtained with the GetDesktopWindow function).

 ● Operation: used to specify the operation to be performed. in

        The "open" operation means to execute the program specified by the FileName parameter, or to open the file or folder specified by the FileName parameter;
        The "print" operation means to print the file specified by the FileName parameter;
        The "explore" operation means to browse the folder specified by the FileName parameter;
        The "edit" operation means to open the document specified by lpFile with an editor. If lpFile is not a document, it will fail;
        The "find" operation means to search the directory specified by lpDirectory;
        The "properties" action represents the display properties
          runas requests to run with administrator privileges, such as running an exe with administrator privileges
          When the parameter is set to nil, it means to perform the default operation "open".

 ●FileName: used to specify the file name to be opened, the program file name to be executed or the folder name to be browsed.

 ● Parameters: If the FileName parameter is an executable program, this parameter specifies the command line parameter, otherwise this parameter should be nil or PChar(0).

 ●Directory: used to specify the default directory.

 ●ShowCmd: If the FileName parameter is an executable program, this parameter specifies the initial display mode of the program window, otherwise this parameter should be set to 0.

 If the ShellExecute function is called successfully, the return value is the instance handle of the executed program. If the return value is less than 32, an error occurred.

            SW_HIDE hides the window and gives a window in the active state
            SW_MINIMIZE Minimize the window, give a window in the active state
            SW_RESTORE Displays a window with its original size and position, while making it active
            SW_SHOW Shows a window at its current size and position, and makes it active
            SW_SHOWMAXIMIZED maximizes the window and activates it
            SW_SHOWMINIMIZED minimizes the window and activates it
            SW_SHOWMINNOACTIVE minimizes a window without changing the active window
            SW_SHOWNA Show a window with its current size and position, without changing the active window
            SW_SHOWNOACTIVATE Shows a window with the most recent size and position without changing the active window
            SW_SHOWNORMAL is the same as SW_RESTORE
 
 
 
  

Reference link: https://blog.csdn.net/primer_programer/article/details/1968171

                 https://blog.csdn.net/yangyang031213/article/details/64121911

2.sprntf_s function

      int sprintf_s(char *restrict buffer, rsize_t bufsz, const char *restrict format, ...);

Formatting data to a string, sprintf_s() is a safe version of sprintf() that avoids the risk of overflow in sprintf() by specifying the buffer length.

 You can output the result of the calling program to a file

Reference link: https://www.cnblogs.com/dirt2/p/6104198.html

                https://blog.csdn.net/tigernana/article/details/6916491

                https://blog.csdn.net/xhjrita/article/details/51209138

3. WinExec function
///for winexec
#include <windows.h>
#include <shellapi.h>

The WinExec function can easily execute a program in an application. Executing a standard program is generally used:

         WinExec('C:\WINDOWS\NOTEPAD.EXE', SW_SHOWNORMAL);


The second parameter is to control the display mode of the main window of the program. WinExec can only execute the exe file, but the WinExec function cannot control the main program to wait for the end of the exe program. WinExec is an old function of Windows, and now CreateProcess can replace almost all functions of WinExe.
 
 
 
  
 

Reference link: https://blog.csdn.net/ddffr/article/details/52895457

                https://blog.csdn.net/zwq815/article/details/56484076
                https://www.cnblogs.com/acloud/archive/2012/08/30/2663395.html
                https://blog.csdn.net/linchaolong/article/details/43943479 [C/C++] Execute the instruction and get the output

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518089&siteId=291194637