user3317
user3317

Reputation:

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

Upvotes: 0

Views: 2792

Answers (2)

valdo
valdo

Reputation: 12951

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.

Upvotes: 0

frast
frast

Reputation: 2740

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

Upvotes: 1

Related Questions