简体   繁体   English

运行ShellExecuteEx后获取线程退出代码

[英]Get Thread exit code after running ShellExecuteEx

How can the exit code of the main thread be retrieved, after having run ShellExecuteEx() in asychronous mode? 在异步模式下运行ShellExecuteEx()之后,如何检索主线程的退出代码?

The process exit code can simply be retrieved as follows: 可以简单地按以下方式检索流程退出代码:

SHELLEXECUTEINFO execInfo;
execInfo.cbSize = sizeof(SHELLEXECUTEINFO);
execInfo.fMask = SEE_MASK_NOASYNC;

ShellExecuteEx(&execInfo);

/* Get process exit code. */
DWORD processExitCode;
GetExitCodeProcess(execInfo.hProcess, &processExitCode);

But how can the exit code of the main thread be retrieved? 但是如何获取主线程的退出代码? What should be passed to GetExitCodeThread()? 什么应该传递给GetExitCodeThread()?

主线程的退出代码等于进程IMHO的退出代码。

In order to get the exit code of the primary process thread - one has to obtain its HANDLE . 为了获取主进程线程的退出代码,必须获取其HANDLE Unfortunately ShellExecuteEx doesn't return you this (it returns only the HANDLE of the newly created process). 不幸的是, ShellExecuteEx不会返回此值(它仅返回新创建的进程的HANDLE )。

One could also enumerate all the threads in a particular process and open their handles ( OpenThread ). 还可以枚举特定进程中的所有线程并打开它们的句柄( OpenThread )。 Thus, you could create a process in a "suspended" state, get the handle of its only thread (which didn't start execution yet), and then go on. 因此,您可以创建处于“挂起”状态的进程,获取其唯一线程(尚未开始执行)的句柄,然后继续。

Alas, ShellExecuteEx neither allows you to create a new process in a suspended state. Execute, ShellExecuteEx都不允许您在挂起状态下创建新进程。

So that I don't see a clean way to achieve what you want. 因此,我看不到一种实现所需目标的干净方法。 I'd suggest the following: 我建议以下内容:

  1. Why would you want the exit code of the primary thread anyway? 您为什么仍要主线程的退出代码? Perhaps the exit code of the process will be enough? 也许该过程的退出代码就足够了吗?
  2. Consider using CreateProcess . 考虑使用CreateProcess It has the needed functionality. 它具有所需的功能。
  3. Some dirty tricks may help, like injecting DLLs into the newly created process (hooking) and etc. 一些肮脏的技巧可能会有所帮助,例如将DLL注入到新创建的进程中(挂钩)等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM