Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a sub-process' return code

Tags:

c++

winapi

What is the method of getting the return value from spawning a sub-process within windows? It looks like ShellExecute() is simpler to use than is CreateProcess(), but from the reading I've done thus far, neither indicate how to check the return value of the spawned process. How is that done?

Thanks, Andy

like image 644
Andrew Falanga Avatar asked Feb 20 '12 23:02

Andrew Falanga


People also ask

What is return code in subprocess?

Return Value of the Call() Method from Subprocess in PythonThe Python subprocess call() function returns the executed code of the program. If there is no program output, the function will return the code that it executed successfully. It may also raise a CalledProcessError exception.

How do I find the return code in Python?

From subprocess 's man page: check_output(*popenargs, **kwargs) Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute.

How do you print exit code in Python?

sys. exit(0) is the correct way to send the return code to your shell.


2 Answers

To acquire the exit code of a process on Windows you can use GetExitCodeProcess().

Example application that accepts the process id as an argument and waits for five seconds for it to complete and then acquires its exit code:

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;
}
like image 108
hmjd Avatar answered Nov 11 '22 17:11

hmjd


Here is a complete code based in http://msdn.microsoft.com/en-us/library/windows/desktop/ms682512%28v=vs.85%29.aspx and the solution of 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;
}

(runs as VS2005 console application in windows XP)

like image 44
cesargastonec Avatar answered Nov 11 '22 19:11

cesargastonec