永发信息网

windows下C++如何创建新进程(注意是进程不是线程)?

答案:2  悬赏:30  手机版
解决时间 2021-01-25 07:47
  • 提问者网友:美人性情
  • 2021-01-24 18:22
windows下C++如何创建新进程(注意是进程不是线程)?
最佳答案
  • 五星知识达人网友:上分大魔王
  • 2021-01-24 18:37
API CreateProcess(...) 例子MSDN上就有。
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}

// Start the child process. if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}

// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
全部回答
  • 1楼网友:夜余生
  • 2021-01-24 20:11
不要使用同步线程;可以通过异步消息机制,在子线程建立消息环进行“顺序”处理(这需要根据你的实际业务需求来决定)或者每次开启单独的子线程;如果你描述的是偶尔发生的情况,那么怎么处理都是可以的(只要不是同步线程),如果情况是频繁发生甚至一直这样,那么你应该调整你的业务逻辑。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯