CreateProcess()无法正常工作

bos*_*man 0 c++ winapi createprocess

我正在使用此代码启动我的程序

     int _tmain(int argc, _TCHAR* argv[])
{
    STARTUPINFO cif;
    ZeroMemory(&cif,sizeof(STARTUPINFO));
    PROCESS_INFORMATION pi;
    if (CreateProcess(L"C:\\test\\test.exe",NULL,
        NULL,NULL,FALSE,CREATE_UNICODE_ENVIRONMENT,NULL,NULL,&cif,&pi)==TRUE)
    {
        cout << "process" << endl;
        cout << "handle " << pi.hProcess << endl;
    }
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

程序正常启动,但立即失败(没有响应并失败).CreateProcess返回true.当我从代码启动test.exe时,它可以正常工作.

Joe*_*sky 5

您必须将STARTUPINFO和PROCESS_INFORMATION的内存归零,并且必须设置STARTUPINFO结构的cb字段.

复制Microsoft文档中示例代码是一个很好的起点.