如何获取ShellExecute调用的exe的返回值

avatar
作者
猴君
阅读量:4

要获取ShellExecute调用的exe的返回值,可以使用ShellExecuteEx函数来替代ShellExecute函数,并使用PROCESS_INFORMATION结构来获取进程的句柄和ID。然后,使用WaitForSingleObject函数等待进程的结束,最后使用GetExitCodeProcess函数获取进程的返回值。

以下是示例代码:

#include <windows.h> #include <shellapi.h> int main() { SHELLEXECUTEINFO sei = { 0 }; sei.cbSize = sizeof(SHELLEXECUTEINFO); sei.fMask = SEE_MASK_NOCLOSEPROCESS; sei.lpVerb = L"open"; sei.lpFile = L"C:\\path\\to\\your.exe"; sei.lpParameters = L"parameters"; sei.nShow = SW_HIDE; if (ShellExecuteEx(&sei)) { WaitForSingleObject(sei.hProcess, INFINITE); DWORD exitCode = 0; GetExitCodeProcess(sei.hProcess, &exitCode); // 使用exitCode来获取返回值 CloseHandle(sei.hProcess); } return 0; } 

在这个示例代码中,我们创建了一个SHELLEXECUTEINFO结构,并设置了需要执行的exe的路径,参数等信息。然后使用ShellExecuteEx函数来执行命令,并获取进程的句柄和ID。接着,使用WaitForSingleObject函数等待进程的结束,使用GetExitCodeProcess函数获取进程的返回值,最后关闭进程的句柄。

请注意,ShellExecuteEx函数在Windows XP及更高版本上可用,如果你的应用程序需要兼容更早的Windows版本,可以考虑使用CreateProcess函数来替代ShellExecuteEx函数。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!