Psychic debugging: Why doesn't my program show up when I call ShellExecute?

Raymond Chen

This question came in from a customer (paraphrased):

If I run my program from the command prompt, it works great, but if I run it from my launcher via ShellExecuteEx, it never appears.

See how good your psychic powers are at solving this problem before I give you the second question that gives away the answer.

Any luck?

Here’s a second question from a different source (but which coincidentally came in the same day).

I’m trying to use ShellExecute to open a document. The function succeeds (returns a value greater than 32), but I don’t get anything on the screen.

if (ShellExecute(Handle, NULL, FileName, NULL,
    NULL, NULL) <= (HINSTANCE)32) ...

The problem the second person is having lies in the last parameter to the ShellExecute function. It’s nShowCmd, which is supposed to be an SW_* value, but which this person is passing as NULL. It so happens, that the value zero corresponds to SW_HIDE, which explains why the program doesn’t appear: You told it to run hidden!

Now go back to the first problem. Do you see what the person most likely did wrong? The code probably went like this:

SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.hwnd = hwnd;
sei.lpVerb = TEXT("open");
sei.lpFile = pszFile;
ShellExecuteEx(&sei);

Since the sei.nShow member was not explicitly set, the value was implicitly set to zero by the incomplete initializer. And as we noted above, zero means SW_HIDE.

It turns out my psychic debugging was correct. That was indeed the source of the first person’s problem. Now you can use your psychic powers, too.

0 comments

Discussion is closed.

Feedback usabilla icon