One-day Windows API training (87) CreateProcess Function
People need to process more and more complex information, often in an application is not enough to deal with, so there are multiple applications to work together to process the same thing. Of course, separate processing of multiple applications is easier to develop, and the complexity of applications is quickly reduced. For example, when developing a bank transaction system, there is a master program generated by the report, and there are many small and different report generation programs. In this way, you need to create a small report program from the main program to run. To create a process to run, you must use the CreateProcess function.
The CreateProcess function declaration is as follows:
Winbaseapi
Bool
Winapi
Createprocessa (
_ In_opt lpcstr lpapplicationname,
_ Inout_opt lpstr lpcommandline,
_ In_opt lpsecurity_attributes lpprocessattributes,
_ In_opt lpsecurity_attributes lpthreadattributes,
_ In bool binherithandles,
_ In DWORD dwcreationflags,
_ In_opt lpvoid lpenvironment,
_ In_opt maid directory,
_ In lpstartupinfoa lpstartupinfo,
_ Out lpprocess_information lpprocessinformation
);
Winbaseapi
Bool
Winapi
Createprocessw (
_ In_opt lpcwstr lpapplicationname,
_ Inout_opt lpwstr lpcommandline,
_ In_opt lpsecurity_attributes lpprocessattributes,
_ In_opt lpsecurity_attributes lpthreadattributes,
_ In bool binherithandles,
_ In DWORD dwcreationflags,
_ In_opt lpvoid lpenvironment,
_ In_opt maid directory,
_ In lpstartupinfow lpstartupinfo,
_ Out lpprocess_information lpprocessinformation
);
# Ifdef Unicode
# Define CreateProcess createprocessw
# Else
# Define CreateProcess createprocessa
# Endif //! Unicode
Lpapplicationname is the name of the application.
Lpcommandline is a command line parameter.
Lpprocessattributes is a process attribute.
Lpthreadattributes is a thread attribute.
Whether binherithandles inherits the attributes of the parent process.
Dwcreationflags is the creation flag.
Lpenvironment is an environment variable.
The lpcurrentdirectory is the current directory.
Lpstartupinfo is the information sent to the new process.
Lpprocessinformation is the information returned by the process.
An example of calling a function is as follows:
#001 // create a process.
#002 // Cai junsheng 2007/12/11 QQ: 9073204 Shenzhen
#003 void testcreateprocess (void)
#004 {
#005 // clear the structure.
#006 startupinfo SINFO;
#007 process_information pinfo;
#008
#009 zeromemory (& SINFO, sizeof (SINFO ));
#010 SINFO. cb = sizeof (SINFO );
#011 SINFO. dwflags = startf_useshowwindow;
#012 SINFO. wshowwindow = sw_shownormal;
#013
#014 zeromemory (& pinfo, sizeof (pinfo ));
#015
#016 // create a process.
#017 if (! : CreateProcess (_ T ("wincpp.exe "),
#018 null,
#019 null,
#020 null,
#021 false,
#022 0,
#023 null,
#024 null,
#025 & SINFO,
#026 & pinfo)
#027)
#028 {
#029 // output error information.
#030 const int nbufsize = 512;
#031 tchar chbuf [nbufsize];
#032 zeromemory (chbuf, nbufsize );
#033
#034 wsprintf (chbuf, _ T ("CreateProcess failed (% d)./N"), getlasterror ());
#035 outputdebugstring (chbuf );
#036 return;
#037}
#038
#039
#040 // and other processes are disabled.
#041 waitforsingleobject (pinfo. hprocess, infinite );
#042
#043 // shut down the process and thread handle.
#044 closehandle (pinfo. hprocess );
#045 closehandle (pinfo. hthread );
#046
#047}