PROCESS_INFORMATION piProcInfo;
ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
bsuccess = ::CreateProcess(NULL, 
TEXT("Client.exe"),   // command line 
NULL,          // process security attributes 
NULL,          // primary thread security attributes 
TRUE,          // handles are inherited 
0,             // creation flags 
NULL,          // use parent's environment 
NULL,          // use parent's current directory 
&siStartInfo,  // STARTUPINFO pointer 
&piProcInfo);  // receives PROCESS_INFORMATION
当断点走到最后一行时,报有未处理异常,是怎么回事呢?

解决方案 »

  1.   

    MSDN例子代码:#include <windows.h>
    #include <stdio.h>void main( VOID )
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;    ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );    // Start the child process. 
        if( !CreateProcess( NULL,   // No module name (use command line). 
            TEXT("MyChildProcess"), // 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 );
    }
      

  2.   

    siStartInfo 没有赋值就使用
      

  3.   

    前面加上两行代码:
    ZeroMemory(&siStartInfo, sizeof(siStartInfo));
    siStartInfo.cb = sizeof(siStartInfo);
      

  4.   

    siStartInfo.cb = sizeof(siStartInfo);
    这个是必须设置的
      

  5.   

    谢谢大家啦,可能是我没说清楚,下面是全部代码:
    #include <iostream>
    #include <Windows.h>
    using namespace std;
    int main()
    {
    STARTUPINFO        siStartInfo; 
    PROCESS_INFORMATION piProcInfo;
    SECURITY_ATTRIBUTES saAttr;           //安全属性 saAttr.bInheritHandle = true;         //可继承
    saAttr.nLength       =  sizeof(SECURITY_ATTRIBUTES);
    saAttr.lpSecurityDescriptor = NULL;   //匿名PIPE,此属性没用 char readBuf[100];
    memset(readBuf,'0',sizeof(readBuf)); DWORD readNum;
    HANDLE hRead; //读句柄
    HANDLE hWrite; //写句柄 BOOL bsuccess = ::CreatePipe(&hRead, &hWrite, &saAttr, 0); //创建匿名管道
    if (bsuccess)
    {
    cout<<"管道创建成功"<<endl;
    }
    else
    {
    cout<<"管道创建失败"<<endl;
    }
    bsuccess = false; HANDLE hTemp = ::GetStdHandle(STD_OUTPUT_HANDLE); //获得本进程的标准输出 ::SetStdHandle(STD_OUTPUT_HANDLE, hWrite); //设置标准输出到匿名管道
        
    ::GetStartupInfo(&siStartInfo); //获取本进程的STARTUPINFO结构信息
    //-------------------------------------------------------------------------
    ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) ); ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); siStartInfo.cb = sizeof(STARTUPINFO); 
    siStartInfo.hStdError = hWrite;
    siStartInfo.hStdOutput = hWrite;
    siStartInfo.hStdInput = hRead;
    /*siStartInfo.dwFlags |= STARTF_USESTDHANDLES;*/
    //-------------------------------------------------------------------------
    //创建一个子线程
    bsuccess = ::CreateProcess(NULL, 
    TEXT("Client.exe"),   // command line 
    NULL,          // process security attributes 
    NULL,          // primary thread security attributes 
    TRUE,          // handles are inherited 
    0,             // creation flags 
    NULL,          // use parent's environment 
    NULL,          // use parent's current directory 
    &siStartInfo,  // STARTUPINFO pointer 
    &piProcInfo);  // receives PROCESS_INFORMATION ::SetStdHandle(STD_OUTPUT_HANDLE, hTemp); //恢复父线程的标准输出
    ::CloseHandle(hWrite);  //关闭写端口,以便读取数据 while (ReadFile(hRead, readBuf, 100, &readNum, NULL)) //读取内容
    {
    readBuf[readNum] = '\0';
    cout<<"从"<<readBuf<<"管道中读取数据字节数为:"<<readNum<<endl;
    }
    if (GetLastError() == ERROR_BROKEN_PIPE)
    {
    cout<<"管道进程被关闭"<<endl;
    }
    else
    {
    cout<<"读数据错误,错误代码为:"<<GetLastError()<<endl;
    }
    return 0;
    }
    管道能创建,就是启线程时有异常
      

  6.   

    /*siStartInfo.dwFlags ¦= STARTF_USESTDHANDLES;*/
    这行怎么注释掉了?
    是当前进程异常还是新启动的进程异常?
    另外,CreateProcess的第2参数建议改成一个字符数组:
    TCHAR cmdLine[100] = TEXT("Client.exe");
    CreateFile(NULL, cmdLine, ……
      

  7.   


    CreateProcess的第2参数改成一个字符数组:
    TCHAR cmdLine[100] = TEXT("Client.exe");
    好用了,谢谢啊!