C++创建子进程

函数原型

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;
}

参考:
API函数 CreateProcess

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qzy0621

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值