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:
- Why would you want the exit code of the primary thread anyway? Perhaps the exit code of the process will be enough?
- Consider using
CreateProcess
. It has the needed functionality. - Some dirty tricks may help, like injecting DLLs into the newly created process (hooking) and etc.
精彩评论