【问题标题】:ShellExecuteEx and getexitcodeprocessShellExecuteEx 和 getexitcode 进程
【发布时间】:2020-01-09 16:28:50
【问题描述】:

使用 ShellExecuteEx(..) 启动 python 脚本和 python 脚本从 python main 返回一个值,使用 sys.exit(0) 成功或其他一些错误值。如何读取 python 脚本退出代码?

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

Python 代码:

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

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

C++ 代码:

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();
        }

在此代码中作为 Python 脚本退出 15,我希望从 GetExitCodeProcess(rSEI.hProcess, &dwCode) 的 dwCode 中读取 15?

感谢您对此提供的所有帮助...

【问题讨论】:

  • 起初ShellExecuteEx 这里绝对不相关(为什么不使用CreateProcessW?)。 if GetExitCodeProcess return true - dwCode 是退出进程的真实代码。来自哪个进程的另一个问题.. 可能是“python.Exe” exec 子进程,您的脚本在哪里运行.. 关于 pyton 不知道
  • Windows 只会报告"python.exe" 运行成功,但是python 模块可能已经失败。确保它正在运行。还有while (PeekMessage...)的目的是什么?
  • 给定的 Python 代码将失败,因为您没有导入时间或系统,并且 logger 也未定义。使用 CreateProcess 来,嗯,CreateProcess。

标签: python c++ winapi shellexecuteex


【解决方案1】:
  1. 正如 cmets 所提到的,您的 python 脚本失败了。

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

  2. 将路径值赋予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
    {
        DWORD lastError = GetLastError();
    }
}

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多