LosBlancoo LosBlancoo - 1 year ago 262
C Question

Why ShellExecuteEx does not set value of SHELLEXECUTEINFO.hProcess?

Why does

is still
after successfully launching an executable with ShellExecuteEx while if I launch that executable with
is set to correct handle of executable?

I have to launch that executable with using ShellExecuteEx because it has to be elevated. Executable that I'm launching is
application created by me.

memset(&shellExecuteInfo, 0, sizeof(SHELLEXECUTEINFOW));
shellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFOW);
shellExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shellExecuteInfo.hwnd = NULL;
shellExecuteInfo.lpFile = pathToExe;
shellExecuteInfo.lpDirectory = NULL;
shellExecuteInfo.nShow = SW_HIDE;
shellExecuteInfo.lpVerb = L"runas";
shellExecuteInfo.hInstApp = NULL;
shellExecuteInfo.lpParameters = NULL;

Answer Source

I believe that the reason is that when you use the runas verb the process that is launched is that which shows the UAC prompt. That in turn is what creates the elevated process. However the call to ShellExecuteEx returns immediately and then the elevated process is created (or perhaps not) some time later. So ShellExecuteEx doesn't have the process handle you need and returns NULL.

You might wonder why ShellExecuteEx doesn't wait for the elevated process to be launched. If it did so then the wait could be significant. The user need not process the UAC dialog immediately. And the design of ShellExecuteEx is that it does not block.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download