February 10th, 2014

Execute a file as if it were a program, even though its extension is not EXE

Today’s Little Program executes a file as if it were a program, even though its extension is not EXE. The idea here is to prevent somebody from running your program by accident, so you give it an extension like .MOD. This is great for preventing somebody from running the program by mistake, but how do you do it on purpose?

#define STRICT
#include <windows.h>
#include <shellapi.h>
int WINAPI WinMain(
    HINSTANCE hinst, HINSTANCE hinstPrev,
    LPSTR lpCmdLine, int nCmdShow)
{
  SHELLEXECUTEINFO sei = { 0 };
  sei.cbSize = sizeof(sei);
  sei.nShow = SW_SHOWNORMAL;
  sei.lpFile = TEXT("C:\\full\\path\\to\\program.mod");
  sei.fMask = SEE_MASK_CLASSNAME;
  sei.lpVerb = TEXT("open");
  sei.lpClass = TEXT("exefile");
  ShellExecuteEx(&sei);
  return 0;
}

We’re merely using the lpClass member of the SHELL­EXECUTE­INFO structure to force the file to be interpreted as the type we specify, overriding the default type inference code.

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.

Feedback