How to start a process under MFC

Source: Internet
Author: User

1.Winexec


This function is the simplest, with only two parameters, the prototype is as follows:

UINT WinExec (

LPCSTR lpCmdLine,//Address of command line

UINT ucmdshow//window style for new application

);


Here's how to use it:

WinExec (_t ("D:\\Program files\\zeecalls\\zeecalls.exe"), sw_showmaximized);

means to open the zeecalls.exe in a maximized way, it should be noted that in the VC, ' \ ' is an escape character that needs to be ' \ \ '.


2.CreateProcess

There are several ways to achieve the purpose of opening other applications in our own applications,

However, we did not get enough information about the new process, nor did we use excessive means to control the details of the new process,

So, if we're going to do this, we need to use the CreateProcess function,

First look at the prototype of this function:

BOOL CreateProcess (

LPCTSTR Lpapplicationname, //Execute program name

LPTSTR lpcommandline, //Parameter line

Lpsecurity_attributes lpprocessattributes,//security properties of the created process, NULL uses default properties

Lpsecurity_attributes lpthreadattributes,//security properties of the created thread, NULL uses default properties

BOOL binherithandles, //Inheritance Flag

DWORD dwcreationflags, //Create flag

LPVOID lpenvironment, //environment variable

LPCTSTR lpcurrentdirectory, //Run the initial directory of the process

Lpstartupinfo Lpstartupinfo, //For setting various properties when creating a child process

Lpprocess_information lpprocessinformation//For receiving information after the process has been created

);


In the above parameters, two more important data structures are used: Startupinfo and process_information. The definitions of these two structures are as follows:


typedef struct _STARTUPINFO {

DWORD CB; Structure length

LPTSTR lpreserved; Keep

LPTSTR LpDesktop; Keep

LPTSTR Lptitle; If the console process is the caption that is displayed

DWORD DwX; Window Horizontal

DWORD DwY; Window Bundle coordinates

DWORD dwxsize; Window width

DWORD dwysize; Window height

DWORD Dwxcountchars; Console window character symbol width

DWORD Dwycountchars; Console window character symbol height

DWORD Dwfillattribute; Console window fill mode

DWORD DwFlags; Create tags

WORD Wshowwindow; The window is displayed as a marker, as in ShowWindow

WORD CbReserved2; Reserved parameters

Lpbyte LpReserved2; Reserved parameters

HANDLE hStdInput; Standard input handle

HANDLE Hstdoutput; Standard output handle

HANDLE Hstderror; Standard error handle

} startupinfo, *lpstartupinfo;

typedef struct _PROCESS_INFORMATION {

HANDLE hprocess; Process Handle

HANDLE Hthread; The main thread handle of the process

DWORD Dwprocessid; ID of the process

DWORD dwThreadID; The main thread ID of the process

} process_information;

As an example, let's look at how to use CreateProcess to open the same file:


Startupinfo si;

memset (&si, 0, sizeof (SI));

Si.cb=sizeof (SI);

Si.wshowwindow = Sw_show;

Si.dwflags = Startf_useshowwindow;


Process_information Pi;

memset (&pi, 0, sizeof (PI));


BOOL Fret=createprocess (

"D:\\Program Files\\zeecalls\\zeecalls.exe"

Null

Null

, FALSE

Null

, 0

Null

Null

, &si

, &pi

);


3.ShellExecute

ShellExecute function prototype and parameter meanings are as follows:

ShellExecute (

HWND hwnd,//parent window Handle

LPCSTR lpoperation,//type of operation

LPCSTR lpfile,//file or path to be manipulated

LPCSTR lpparameters,//Specify parameters to pass when Lpoperation is explore, usually set to null

LPCSTR lpdirectory,//Specify default directory, usually set to null

INT nShowCmd//How to open the file in the usual way or maximize or minimize the display

)

First to include the header file shellapi.h


Examples are as follows:

Call Calculator

ShellExecute (NULL, "open", "calc.exe", null,null,sw_shownormal);


Call Notepad

ShellExecute (NULL, "open", "NOTEPAD"). EXE ", null,null,sw_shownormal);


Open a hyperlink

ShellExecute (HWnd, "open", "http://hexun.com/kenshin2008", null,null,sw_show);


Open path file PathName (file path)

ShellExecute (NULL, "open", pathname,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.

The last parameter, nShowCmd, specifies a parameter for the visibility of the window.

Please use any one of the following constants

Sw_hide Hide window, activate another window

sw_minimize minimized window, activating another window

Sw_restore displays a window with its original size and position, while making it active

Sw_show displays a window with the current size and position, and makes it active

Sw_showmaximized to maximize the window and activate it

sw_showminimized Minimize the window and activate it

Sw_showminnoactive minimizes a window but does not change the active window

Sw_showna displays a window with the current size and position without changing the active window

Sw_shownoactivate displays a window with the nearest size and position without changing the active window

Sw_shownormal is the same as Sw_restore

As can be seen, through the above several different methods can be implemented in the application to open other applications, the above several methods may be a bit troublesome, so we need to choose the most suitable for their own purpose of the way to achieve their goal!


How to start a process under MFC

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.