开发者

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?

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()?


The exit code of the main thread is equal to the exit code of the process IMHO.


In order to get the exit code of the primary process thread - one has to obtain its HANDLE. Unfortunately ShellExecuteEx doesn't return you this (it returns only the HANDLE of the newly created process).

One could also enumerate all the threads in a particular process and open their handles (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.

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. It has the needed functionality.
  3. Some dirty tricks may help, like injecting DLLs into the newly created process (hooking) and etc.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜