2012-02-20 53 views
10

从窗口中获取子进程的返回值的方法是什么?它看起来像ShellExecute()CreateProcess()更容易使用,但是从目前为止所做的阅读中,都没有指出如何检查衍生进程的返回值。这是如何完成的?如何获得子进程的返回码

感谢, 安迪

回答

18

为了获得Windows上的过程中,你可以使用GetExitCodeProcess()的退出代码。

接受进程ID作为参数,并等待5秒,它完成,然后

实施例的应用程序获得其退出代码:

int main(int a_argc, char** a_argv) 
{ 
    int pid = atoi(*(a_argv + 1)); 

    HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid); 

    if (NULL != h) 
    { 
     WaitForSingleObject(h, 5000); // Change to 'INFINITE' wait if req'd 
     DWORD exit_code; 
     if (FALSE == GetExitCodeProcess(h, &exit_code)) 
     { 
      std::cerr << "GetExitCodeProcess() failure: " << 
       GetLastError() << "\n"; 
     } 
     else if (STILL_ACTIVE == exit_code) 
     { 
      std::cout << "Still running\n"; 
     } 
     else 
     { 
      std::cout << "exit code=" << exit_code << "\n"; 
     } 
     CloseHandle(h); 
    } 
    else 
    { 
     std::cerr << "OpenProcess() failure: " << GetLastError() << "\n"; 
    } 

    return 0; 
} 
+3

谢谢。这是我正在寻找的。看起来我必须使用CreateProcess()来开始这件事。该函数填充一个PROCESS_INFORMATION结构,该结构具有我需要用于GetExitCodeProcess()的HANDLE。 – 2012-02-21 17:01:28

+0

当你完成句柄时,一定要使用'CloseHandle'函数关闭它。 – 2014-05-08 10:21:16

+0

@EugeneRyabtsev,谢谢,我忘了补充一点。更新。 – hmjd 2014-05-08 10:30:52

4

下面是基于在http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512%28v=vs.85%29.aspx完整的代码和hmjd的溶液:

#include <stdio.h> 
#include <Windows.h> 

int main() 
{ 
    const size_t stringSize = 1000; 
    STARTUPINFO si; 
    PROCESS_INFORMATION pi; 
    DWORD exit_code; 
    char commandLine[stringSize] = "C:\\myDir\\someExecutable.exe param1 param2"; 
    WCHAR wCommandLine[stringSize]; 
    mbstowcs (wCommandLine, commandLine, stringSize); 

    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) 
     wCommandLine, // 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 -1; 
    } 

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

    GetExitCodeProcess(pi.hProcess, &exit_code); 

    printf("the execution of: \"%s\"\nreturns: %d\n", commandLine, exit_code); 

    // Close process and thread handles. 
    CloseHandle(pi.hProcess); 
    CloseHandle(pi.hThread); 
    return 0; 
} 

(如运行在Windows XP VS2005控制台应用程序)

+0

在第0个命令行参数周围添加引号是明智的,以防有人复制代码段将'... \ my dir \ ...'放在那里。 – 2014-05-08 10:28:54

+0

它运行良好(已被证实)。即使路径包含空格,也不需要额外的引号。相反,如果添加“commandLine”周围的引号,则“CreateProcess”将不起作用。 – cesargastonec 2014-05-09 18:42:15

+1

http://www.verisigninc.com/en_AU/cyber-security/security-intelligence/vulnerability-reports/articles/index.xhtml?id=340 – 2014-05-10 04:36:05