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

Raymond Chen

Raymond

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.

Raymond Chen
Raymond Chen

Follow Raymond   

0 comments

Comments are closed.