ShellExecuteEx and getexitcodeprocess

984 Views Asked by At

Using ShellExecuteEx(..) to lunch a python script and python script returning a value from python main using sys.exit(0) on success or some other error value. How to read a python script exit code?

After launching application waited to complete script by using MsgWaitForMultipleObjects (...) and then calling GetExitCodeProcess(...) some reason I always read value 1 from getExitCodeprocess(..)

Python Code:

def main():
    time.sleep(10)   
    logger.info("************The End**********")
    return (15)

if __name__ == "__main__":
    sys.exit(main())

C++ Code:

SHELLEXECUTEINFO rSEI = { 0 };
    rSEI.cbSize = sizeof(rSEI);
    //rSEI.lpVerb = "runas";
    rSEI.lpVerb = "open";
    rSEI.lpFile = "python.Exe";
    rSEI.lpParameters = LPCSTR(path.c_str());
    rSEI.nShow = SW_NORMAL;
    rSEI.fMask = SEE_MASK_NOCLOSEPROCESS;

    if (ShellExecuteEx(&rSEI))   // you should check for an error here
            ;
        else
            errorMessageID = GetLastError();        //MessageBox("Error", "Status", 0);

        WORD nStatus;
        MSG msg;     // else process some messages while waiting...
        while (TRUE)
        {
            nStatus = MsgWaitForMultipleObjects(1, &rSEI.hProcess, FALSE, INFINITE, QS_ALLINPUT);   // drop through on user activity 

            if (nStatus == WAIT_OBJECT_0)
            {  // done: the program has ended
                break;
            }

            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                DispatchMessage(&msg);
                //MessageBox("Wait...", "Status", 0);
            }

        }  // launched process has exited

        DWORD dwCode=0;
        if (!GetExitCodeProcess(rSEI.hProcess, &dwCode)) //errorvalue
        {
            DWORD lastError = GetLastError();
        }

In this code as Python script exiting with 15, I am expecting to read 15 from dwCode from GetExitCodeProcess(rSEI.hProcess, &dwCode)?

Appreciates all of your help on this...

1

There are 1 best solutions below

0
Drake Wu On
  1. As the comments metioned, your python script fails.

Python Code Sample:

import sys
import time
import logging
import logging.handlers
logger = logging.getLogger("logger")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
logger.addHandler(handler)
def main():
    time.sleep(10)  
    logger.info("************The End**********")
    return (15)

if __name__ == "__main__":
    sys.exit(main())
  1. Name your Python script file with .py as the suffix instead of .Exe, such as "python.py"

  2. Give path value to SHELLEXECUTEINFO.lpDirectory, and set SHELLEXECUTEINFO.lpParameters to NULL here. Or Give the path and file combination to SHELLEXECUTEINFO.lpVerb, like "Path\\python.py"

C++ Code Sample:

#include <windows.h>
#include <iostream>
#include <string>
void main()
{
    int errorMessageID = 0;
    std::string path = "Path";
    SHELLEXECUTEINFO rSEI = { 0 };
    rSEI.cbSize = sizeof(rSEI);
    //rSEI.lpVerb = "runas";
    rSEI.lpVerb = "open";
    rSEI.lpFile = "python.py";
    rSEI.lpParameters = NULL;
    rSEI.lpDirectory = path.c_str();
    rSEI.nShow = SW_NORMAL;
    rSEI.fMask = SEE_MASK_NOCLOSEPROCESS;

    if (!ShellExecuteEx(&rSEI))   // you should check for an error here
        errorMessageID = GetLastError();        //MessageBox("Error", "Status", 0);

    WORD nStatus;
    MSG msg;     // else process some messages while waiting...
    while (TRUE)
    {
        nStatus = MsgWaitForMultipleObjects(1, &rSEI.hProcess, FALSE, INFINITE, QS_ALLINPUT);   // drop through on user activity 

        if (nStatus == WAIT_OBJECT_0)
        {  // done: the program has ended
            break;
        }

        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            DispatchMessage(&msg);
            //MessageBox("Wait...", "Status", 0);
        }

    }  // launched process has exited

    DWORD dwCode = 0;
    if (!GetExitCodeProcess(rSEI.hProcess, &dwCode)) //errorvalue
    {
        DWORD lastError = GetLastError();
    }
}