2

I'm attempting to launch a process from a different process. The mechanism in which this is achieved is not subject to change. Both the launcher and the original process are located in C:\dir.

I'm starting my launcher from a cmd file. The cmd file itself is located somewhere else, and in order for it to find the launcher executable, I'm setting the PATH variable:

set PATH=C:\dir;%PATH%;
launcher.exe

The launcher starts the child process with the following code:

  STARTUPINFO startupInfo;
  startupInfo.cb               = sizeof (STARTUPINFO);
  startupInfo.lpReserved       = 0;
  startupInfo.lpDesktop        = NULL;
  startupInfo.lpTitle          = NULL;
  startupInfo.dwX              = 0;
  startupInfo.dwY              = 0;
  startupInfo.dwXSize          = 0;
  startupInfo.dwYSize          = 0;
  startupInfo.dwXCountChars    = 0;
  startupInfo.dwYCountChars    = 0;
  startupInfo.dwFillAttribute  = 0;
  startupInfo.dwFlags          = _showInForeground ? STARTF_USESHOWWINDOW : 0;
  startupInfo.wShowWindow      = _showInForeground ? 1 : 0;
  startupInfo.cbReserved2      = 0;
  startupInfo.lpReserved2      = 0;

  PROCESS_INFORMATION processInfo;

  BOOL retVal = CreateProcess("child.exe", "", NULL, NULL, FALSE, 
                    _showInForeground ? (CREATE_NEW_CONSOLE | CREATE_DEFAULT_ERROR_MODE) : CREATE_DEFAULT_ERROR_MODE,
                    NULL, NULL, &startupInfo,&processInfo);

It returns 0 and last error is 2, which is File not found.

If it helps, GetCurrentDirectory returns the directory where the cmd is located, not C:\dir. I'm guessing CreateProcess can't find child.exe because PATH is not available to it.

Any ideas how to get this to work?

EDIT: Some good comments with answers (as comments are sometimes overlooked):

Suggestion: set statupInfo.lpDirectory to "c:\dir"

Answer: can't. I'm starting from the cmd because the directory may change.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    set statupInfo.lpDirectory to "c:\\dir" – Lucian Mar 16 '12 at 13:39
  • @Lucian can't. I'm starting from the `cmd` because the directory may change. – Luchian Grigore Mar 16 '12 at 13:41
  • Why mess with the PATH variable at all ? If the cmd file knows the location of the executable to be launched, why not pass the path to the executable as a parameter to your launcher on the command line ? `launcher.exe c:\dir\child.exe` – WaffleSouffle Mar 16 '12 at 13:57
  • @WaffleSouffle this is just one of many `cmd` files generated by a legacy tool which is not subject to change. – Luchian Grigore Mar 16 '12 at 14:03
  • @LuchianGrigore. The launcher process can get its own folder and use that. There is no need to change the path variable or change the working dir. – Deanna Mar 16 '12 at 14:55
  • @Deanna "GetCurrentDirectory returns the directory where the cmd is located, not C:\dir". – Luchian Grigore Mar 16 '12 at 14:57
  • @LuchianGrigore: That's not what I said. Use `GetModuleFileName()` to get the exe path itself. See [this question](http://stackoverflow.com/q/1528298/588306) for more information. – Deanna Mar 16 '12 at 15:05
  • @Deanna ok perhaps I over simplified. Or misinformed. The two aren't *necessarily* in the same directory. They are in directories set in PATH by the script. molbdnilo's answer is perfect because of this. – Luchian Grigore Mar 16 '12 at 15:08

2 Answers2

5

According to MSDN, CreateProcess actually does search PATH, but only if lpApplicationName is NULL and the executable is the first token in lpCommandLine.
In other words it should work if you call CreateProcess(NULL, "child.exe", ...

I haven't tried it though, so YMMV and so on.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

The launcher process can get its own path using GetModuleFileName() that it can use to create the full path to the 2nd executable. There is no need to change the Path environment variable or change the working directoy.

Deanna
  • 23,876
  • 7
  • 71
  • 156