相关文章推荐
帅呆的炒粉  ·  手机模拟器游戏 - 京东·  1 年前    · 
拉风的海龟  ·  git checkout branch ...·  1 年前    · 
俊逸的草稿本  ·  c# - Deserializing ...·  1 年前    · 

ShellExecuteEx和getexitcodeprocess

0 人关注

使用ShellExecuteEx(...)来午餐一个python脚本和python脚本在成功或其他错误值时使用sys.exit(0)从python main返回一个值。如何读取python脚本的退出代码?

在启动应用程序后,通过使用MsgWaitForMultipleObjects(...)等待完成脚本,然后调用GetExitCodeProcess(...),由于某些原因,我总是从getExitCodeprocess(...)读取值1。

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
            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)?

感谢你们在这方面的帮助...

3 个评论
RbMm
起初 ShellExecuteEx 在这里绝对没有关系(为什么不使用 CreateProcessW 呢)。 true - in dwCode 另一个问题,可能是 "python.Exe "执行子进程,你的脚本在那里运行。
Windows只会报告说 "python.exe" 运行成功,但python模块可能已经失败。请确保它正在运行。另外, while (PeekMessage...) 的作用是什么?
所给的 Python 代码会失败,因为你没有导入 time 或 sys,而且 logger 也是未定义的。使用 CreateProcess 来,好吧,CreateProcess。
python
c++
winapi
shellexecuteex
Jammy1
Jammy1
发布于 2019-09-07
1 个回答
Drake Wu
Drake Wu
发布于 2019-09-09
已采纳
0 人赞同
  • As the comments metioned, your python script fails.
  • Python代码样本。

    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())
    
  • .py作为后缀来命名你的Python脚本文件,而不是.Exe,例如"python.py"

  • 将路径值设为SHELLEXECUTEINFO.lpDirectory,并将SHELLEXECUTEINFO.lpParameters设为NULL。 或者把路径和文件组合给SHELLEXECUTEINFO.lpVerb,如"Path\\python.py"

  • C++代码样本。

    #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