函数原型
CreateProcess函数进程创建原型:
#ifdef UNICODE
#define CreateProcess CreateProcessW
#else
#define CreateProcess CreateProcessA
#endif // !UNICODE
BOOL 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_ LPCSTR lpCurrentDirectory,
_In_ LPSTARTUPINFOA lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation
);
BOOL 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_ LPCWSTR lpCurrentDirectory,
_In_ LPSTARTUPINFOW lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation
);
- CreateProcess用来创建一个新的进程和自身主线程,这个新进程运行指定的是可执行文件。若执行失败返回0,可以使用GetLastError函数获得错误的附加信息。
- 前两个参数lpApplicationName和lpCommandLine不能同时为空
- lpApplicationName: 可执行文件的文件名。
若不为空,必须指定文件扩展名,此时lpCommandLine中只放传递给子进程的参数
若为空,lpCommandLine的第一个参数为可执行文件的文件名,若无扩展名默认为exe - lpCommandLine:传递给子进程的参数,以空格分隔
- lpProcessAttributes:进程安全性设置,为NULL时使用默认设置
- lpThreadAttributes:线程安全性设置,为NULL时使用默认设置
- bInheritHandles:当前进程内的句柄是否可以被子进程继承
FALSE: 不可继承
TRUE: 可继承 - dwCreationFlags:为新进程创建一个新的控制台窗口
0: 不创建
CREATE_NEW_CONSOLE: 创建 - lpEnvironment:是否使用本进程的环境变量
- lpCurrentDirectory:是否使用本进程的驱动器和目录
- lpStartupInfo:进程启动的信息,根据需求可配置结构体参数,若无需求可设置为0
- lpProcessInformation:进程创建成功后,返回进程和线程句柄和id的结构体
WaitForSingleObject函数原型
DWORD WaitForSingleObject(
_In_ HANDLE hHandle, //等待进程对象的句柄
_In_ DWORD dwMilliseconds //等待的时间,INFINITE为无限等待
);
只有当进程对象运行结束时,WaitForSingleObject才会被触发
当WaitForSingleObject返回时,通过GetExitCodeProcess(hHandle, &dwExitCode); 得到退出进程的退出码
子进程
子进程,输出到为 E:\ChildProcess.exe
#include <windows.h>
#include <stdio.h>
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Enter Child Process." << std::endl;
std::cout << "argc: "<< argc << std::endl;
for (int i = 0; i < argc; ++i)
{
std::cout << "argv[ " << i << "]: " << argv[i] << std::endl;
}
system("pause");
return 0;
}
主进程
#include <iostream>
#include <windows.h>
int main()
{
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
ZeroMemory(&piProcInfo, sizeof(piProcInfo)); //0填充
ZeroMemory(&siStartInfo, sizeof(siStartInfo));
TCHAR tcharCMDLine[] = TEXT("E:\\ChildProcess.exe a b c d");
//std::string szParam = " a b c d";
//std::string szChildPipUTPath = "E:\\ChildProcess.exe";
//std::string szCMDLine = szChildPipUTPath + szParam;
//TCHAR* tcharCMDLine = MultiByteToWideChar(szCMDLine);
if (0 == CreateProcess(NULL,tcharCMDLine,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&siStartInfo, &piProcInfo); )
{
std::cout << "CreateProcess failed." << std::endl;
}
else
{
std::cout << "CreateProcess succeed." << std::endl;
std::cout << "Child Process id:" << piProcInfo.dwProcessId << std::endl; //新建进程ID
std::cout << "Child Thread id:" << piProcInfo.dwThreadId << std::endl; //新建主线程ID
}
std::cout << "Start wait child Process end." << std::endl;
WaitForSingleObject(piProcInfo.hProcess, INFINITE); //等待子进程结束,否则阻塞
std::cout << "Child Process end." << std::endl;
//if (nullptr != tcharCMDLine)
//{
// delete[] tcharCMDLine;
// tcharCMDLine = NULL;
//}
CloseHandle(piProcInfo.hProcess); //关闭子进程句柄,若提前关闭,WaitForSingleObject不会阻塞
CloseHandle(piProcInfo.hThread); //关闭子进程主线程句柄
system("pause");
return 0;
}
lpCommandLine传入的是多字节,若是string需要转多字节,字符串转多字节可调用以下函数(注意:返回的值需要释放)
TCHAR* MultiByteToWideChar(const std::string& szContent)
{
const char* pcContent = szContent.c_str();
int iContentSize = strlen(pcContent) + 1; //添加\0
int iWCharSize = MultiByteToWideChar(CP_OEMCP, 0, pcContent, iContentSize, NULL, 0);
TCHAR* pWcContent = new wchar_t[iWCharSize];
MultiByteToWideChar(CP_OEMCP, 0, pcContent, iContentSize, pWcContent, iWCharSize);
return pWcContent;
}