ShellExecuteEx和getexitcodeprocess

2024-09-27 00:14:12 发布

您现在位置:Python中文网/ 问答频道 /正文

使用ShellExecuteEx(..)获取python脚本,并使用从python main返回值的python脚本系统出口(0)成功或其他错误值。如何读取python脚本退出代码?你知道吗

在启动应用程序后,我一直在使用MsgWaitForMultipleObjects(…)等待完成脚本,然后调用getexitdeprocess(…),原因是我总是从getexitdeprocess(…)读取值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();
        }

在这段代码中,作为带15的Python脚本,我希望从GetExitCodeProcess的dwCode中读取15(rSEI.h进程,&dwCode)?你知道吗

感谢你在这方面的帮助。。。你知道吗


Tags: 代码脚本ifmainmsgelsewhilegetlasterror
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:12
  1. 如注释所示,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作为后缀而不是.Exe命名Python脚本文件,例如"python.py"

  2. 将路径值赋给SHELLEXECUTEINFO.lpDirectory,并在此处将SHELLEXECUTEINFO.lpParameters设置为NULL。 或者将路径和文件组合赋给SHELLEXECUTEINFO.lpVerb,如"Path\\python.py"

<强> C++代码示例:>/P>

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

相关问题 更多 >

    热门问题