用CreateProcess执行一条CMD命令,怎么样能得到命令的返回信息呢?
谢谢!

解决方案 »

  1.   

    在 MSDN 里查找这个标题 “Creating a Child Process with Redirected Input and Output”提供了一个例程
      

  2.   

    CreateProcess( ... );waitforsingleobject( ... );
    GetExitCodeProcess( ... );
      

  3.   

    int main()
    {
    //char *a = getneedreplacestring( "public static void main(){ printf ( \"%s\\n\", \"hello, world!\" );}", "hello", "-HELLO-" );
    //printf( "%s\n", a );
    //delete []a;
    //a = NULL;
        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). 
            "d:\\bb.exe", // 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.\n" );
    return 1;
        }    // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );
    unsigned long l;
    GetExitCodeProcess( pi.hProcess, &l );
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    printf( "exited with %d\n", l );
    return 0; 
    }aa.c
    main()
    {
         return 0;
    }bb.c
    main()
    {
         return 1;
    }
      

  4.   

    dir > output.txtoutput.txt中就是DIR后显示的内容了.这样也许更简便.