poday
poday

Reputation: 1

Is there a way to launch an executable that doesn't have an executable extension?

I have two separate executable files, A.exe & B.dontrun, where A.exe launches B.dontrun after doing some initialization. The two processes then communicate to each other and A.exe exits after B.dontrun exits. This all behaves fine using CreateProcess and passing the executable name as the first argument when B.dontrun is named B.exe but if B.dontrun is named anything else (B.ex_ or B.bin) CreateProcess doesn't return an error, but the process isn't launched either.

I'd like B.dontrun to be named something that doesn't encourage people to run it directly, when they look in the directory they see A.exe and B.dontrun and there isn't confusion of which executable that they should be running.

Upvotes: 0

Views: 192

Answers (3)

Serge Wautier
Serge Wautier

Reputation: 21888

You need to specify the exe name in the cmd line argument rather than in the application name.

This works:

  STARTUPINFO info;
  ZeroMemory(&info, sizeof(info)); info.cb = sizeof(info);
  PROCESS_INFORMATION pi;
  ZeroMemory(&pi, sizeof(pi));
  TCHAR sz[1000]; // Note: lpCommandLine must be writable
  lstrcpy(sz,  L"c:\\users\\serge\\desktop\\notepad.dontrun");
  CreateProcess(NULL, sz, NULL, NULL, FALSE, 0, NULL, NULL, &info, &pi);
  printf("Error = %u\n", GetLastError());

This indeed gives a File not found error (2):

  STARTUPINFO info;
  ZeroMemory(&info, sizeof(info)); info.cb = sizeof(info);
  PROCESS_INFORMATION pi;
  ZeroMemory(&pi, sizeof(pi));
  CreateProcess(L"c:\\users\\serge\\desktop\\notepad.dontrun",
    NULL, NULL, NULL, FALSE, 0, NULL, NULL, &info, &pi);
  printf("Error = %u\n", GetLastError());

Note: Tested on Win7 x64

Upvotes: 1

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145379

At least up till and including Windows XP, the [cmd.exe] command interpreter recognizes a PE executable as such regardless of the filename extension, and runs it.

Which is one reason why it's not a good idea to start a text document with the letters "MZ"... ;-)

And which means that it’s not a good idea to try to prevent execution via filename mangling.

Instead, make the other process a DLL, and launch it via rundll32.

Cheers & hth.,

Upvotes: 2

Scott Chamberlin
Scott Chamberlin

Reputation: 724

You should create the file as hidden.

CreateFile has an attribute you can use FILE_ATTRIBUTE_HIDDEN 2 (0x2) The file is hidden. Do not include it in an ordinary directory listing.

Documentation here: http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

Upvotes: 0

Related Questions