首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShellExecuteEx和getexitcodeprocess处理

ShellExecuteEx和getexitcodeprocess处理
EN

Stack Overflow用户
提问于 2019-09-06 20:08:47
回答 1查看 739关注 0票数 0

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

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

Python代码:

代码语言:javascript
复制
def main():
    time.sleep(10)   
    logger.info("************The End**********")
    return (15)

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

C++代码:

代码语言:javascript
复制
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();
        }

在这段代码中,我希望从dwCode从GetExitCodeProcess(rSEI.hProcess &dwCode)读取15?

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

EN

回答 1

Stack Overflow用户

发布于 2019-09-09 03:22:44

  1. 当注释出现时,您的python脚本将失败。

Python代码示例:

代码语言:javascript
复制
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.py"
  2. 将路径值赋给SHELLEXECUTEINFO.lpDirectory,并在此处将SHELLEXECUTEINFO.lpParameters设置为NULL。或者将路径和文件组合给SHELLEXECUTEINFO.lpVerb,如"Path\\python.py"

C++代码示例:

代码语言:javascript
复制
#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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57828024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档