debugging a process spawned with CreateProcess in Visual Studio debugging a process spawned with CreateProcess in Visual Studio windows windows

debugging a process spawned with CreateProcess in Visual Studio


Plenty of options:

  • You can debug multiple .exes with one instance of the debugger by using Debug + Attach. I however always use two instances of Visual Studio, use Debug + Attach in the second one.

  • You could put a __debugbreak() in the child's code, the JIT debugger window will prompt you to select a debugger.

  • You can use the "Image File Execution Options" registry key to automatically launch a debugger as soon as the child .exe is started. Add a key named HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\YourApp.exe, set its "Debugger" value to "vsjitdebugger.exe"

  • There is a VS add-in that makes it easier yet, haven't tried it myself yet.


You can temporarily put in a call to DebugBreak() somewhere in the startup code of your child process. This will cause Windows to prompt you if you want to debug that process.


You could put a global named mutex around your CreateProcess call, and then try to grab the mutex in the child process. If you then put a breakpoint on the CreateProcess call, you should have time to attach to the child before it does anything substantial.

Note that you would miss anything that happens before main in the child process.

edit: As an example, something like this, untested:

// parent.cppHANDLE hMutex = ::CreateMutex(NULL, TRUE, "Global\\some_guid");::CreateProcess(...);::ReleaseMutex(hMutex); // breakpoint here::CloseHandle(hMutex);// child.cppint main(...){  HANDLE hMutex = ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, "Global\\some_guid");  ::WaitForSingleObject( hMutex, INFINITE );  ::CloseHandle(hMutex);  ...}

You would probably want to wrap it with #if _DEBUG or environment variable checks.