0

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.

poday
  • 1
  • How about hiding it in a sub "bin" folder? Also, you can check if it was spawned by A.exe I think. – Xeo Jun 24 '11 at 03:20
  • I already do the verification to ensure that B.dontrun was launched by A.exe (if the communication wasn't set up, if a special cmd line flagged wasn't passed, toolhelp.dll, etc) but I'd like to avoid the poor user experience of "I keep trying to launch B.exe but nothing happens!" – poday Jun 24 '11 at 04:01
  • 1
    @poday: Don't. Stuff it in a "bin" folder and you should be fine, nobody is worrying about that. Look at major applications, nobody hinders you to start the sub-exes. – Xeo Jun 24 '11 at 04:47
  • please post some code on how you call CreateProcess. It's important that you pass the full path to the renamed exe file in the first parameter, not the second! And CreateProcess WILL return an error if the process can't be created. – Stefan Jun 24 '11 at 11:34
  • @Stefan: No. Passing the filename in the 1st arg is actually the problem here. See my answer below – Serge Wautier Jun 24 '11 at 12:36
  • @poday: Why is the user experience "nothing happens". Pop up a message box explaining that the program can't be launched directly. I've seen this numerous times. – Ben Voigt Jun 24 '11 at 12:38

3 Answers3

2

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.,

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

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

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
0

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

Scott Chamberlin
  • 724
  • 2
  • 8
  • 23
  • Unfortunately I don't have control over the deployment of the executable files. I could copy B.dontrun to B.exe but that would require the user to have write permission which isn't guaranteed. – poday Jun 24 '11 at 03:57