C++/mfc How to start another application and get its process ID

Source: Internet
Author: User

ShellExecute (Hwnd:hwnd; {Specify parent Window handle} Operation:pchar; {Specify the action, for example: Open, RunAs, print, edit, explore, find[2]} Filename:pchar; {Specify the file or program to open} Parameters:pchar; {Specify parameters for the program you want to open, or nil if you open the file} Directory:pchar; {Default Directory} Showcmd:integer {Open option}): HINST;

Use to include #include <shellapi.h>

ShellExecute function prototype and parameter meanings are as follows:

ShowCmd parameter Optional value: sw_hide = 0; Hidden

Sw_shownormal = 1; {display with the nearest size and position, activate}

Sw_normal = 1; {Same Sw_shownormal}

sw_showminimized = 2; {Minimized, active}

sw_showmaximized = 3; {Maximize, activate}

Sw_maximize = 3; {Same sw_showmaximized}

Sw_shownoactivate = 4; {display with the nearest size and position, not activated}

Sw_show = 5; {Same Sw_shownormal}

Sw_minimize = 6; {minimized, not activated}

sw_showminnoactive = 7; {Same Sw_minimize}

Sw_showna = 8; {Same Sw_shownoactivate}

Sw_restore = 9; {Same Sw_shownormal}

Sw_showdefault = 10; {Same Sw_shownormal}

Sw_max = 10; {Same Sw_shownormal}

3 Return value Edit

Successful execution returns the application handle

The returned HINSTANCE can convert it to an integer (%d) and compare its error code to a value greater than or less than 32 or to compare it

A return value greater than 32 indicates successful execution

A return value of less than 32 indicates an execution error

Possible errors in the return value are: = 0 {out of memory}

Error_file_not_found = 2; {File name error}

Error_path_not_found = 3; {Path name error}

Error_bad_format = 11; {EXE file is invalid}

Se_err_share = 26; {A sharing error has occurred}

Se_err_associncomplete = 27; {The file name is not complete or invalid}

Se_err_ddetimeout = 28; Timeout

Se_err_ddefail = 29; {DDE transaction failed}

Se_err_ddebusy = 30; {The DDE transaction cannot be completed while the other DDE transaction is being processed}

Se_err_noassoc = 31; {There are no associated applications}

Example Editor

Call Calculator

" Open " " calc.exe ", NULL, NULL, SW_SHOWNORMAL);

Call Notepad

ShellExecute (NULL,"open","NOTEPAD"). EXE", NULL, NULL, SW_SHOWNORMAL);

HWnd: Used to specify the parent window handle. When an error occurs in the function call procedure, it acts as the parent window of the Windows message window. For example, you can set it to the application main window handle, which is application.handle, or you can set it as the desktop window handle (obtained with the GetDesktopWindow function).

Operation: Used to specify the action to be made. Where the "open" operation means that the program specified by the filename parameter is executed, or the file or folder specified by the filename parameter is opened, and the "print" operation means that the file specified by the filename parameter is printed; "Explore" The action represents browsing the folder specified by the filename parameter. When the parameter is set to nil, the default action "open" is executed.

FileName: Used to specify the file name to open, the name of the program file to execute, or the folder name to browse.

Parameters: If the filename parameter is an executable program, this parameter specifies a command-line argument, 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 how the program window is initially displayed, otherwise this parameter should be set to 0.

If the ShellExecute function call succeeds, the return value is the instance handle of the executing program. If the return value is less than 32, it indicates an error occurred.

The above is only the standard usage of the ShellExecute function, and the following describes its special usage.

C++/mfc How to start another application and get its process ID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.