ShellExecuteEx and getexitcodeprocess
ShellExecuteEx and getexitcodeprocess
使用 ShellExecuteEx(..( 来午餐一个 python 脚本和 python 脚本在成功或其他错误值时使用 sys.exit(0( 从 python main 返回一个值。如何读取python脚本退出代码?
启动应用程序后,使用 MsgWaitForMultipleObjects (...( 等待完成脚本,然后调用 GetExitCodeProcess(...( 一些原因,我总是从 getExitCodeprocess(..( 读取值 1
蟒蛇代码:
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
?感谢您对此的所有帮助...
- 正如评论所指的那样,你的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())
用
.py
而不是.Exe
命名你的Python脚本文件,例如"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
{
DWORD lastError = GetLastError();
}
}
相关文章:
- 没有找到相关文章