October 23rd, 2006

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

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.