Simple things you can do with the ShellExecuteEx function

Raymond Chen

Here’s a tiny little program:

#include <windows.h>
#include <shellapi.h>

int __cdecl main(int argc, char **argv) { if (argc == 3) { SHELLEXECUTEINFO sei = { sizeof(sei) }; sei.fMask = SEE_MASK_FLAG_DDEWAIT; sei.nShow = SW_SHOWNORMAL; // added 27 Nov sei.lpVerb = argv[1]; sei.lpFile = argv[2]; ShellExecuteEx(&sei); } return 0; }

This is a little program that takes two parameters, the first being the verb and the second the file upon which to execute the verb. Notice that since we exit immediately, we need to set the SEE_MASK_FLAG_DDEWAIT flag: Normally, the ShellExecuteEx function assumes that there will be a message pump running after it returns. This allows it to return quickly and continue any necessary DDE conversations as the responses arrive from the DDE server. But if the thread is exiting or if the thread is not a GUI thread (both of which are true here), you want to suppress this behavior because there is no message pump around to complete the DDE conversation. Setting the SEE_MASK_FLAG_DDEWAIT flag indicates that the ShellExecuteEx function should finish its DDE conversation before it returns.

Anyway, I wrote this little program to illustrate two of the canonical verbs that you can use. It seems the people don’t realize that ShellExecuteEx can be used to perform these actions, since it gets asked a lot…

  • shex find %windir%
    Opens the search window with a specified folder as the default “Search in” location.
  • shex openas C:\AUTOEXEC.BAT
    Displays the “Open with” dialog for a file.

0 comments

Discussion is closed.

Feedback usabilla icon