debugging a process spawned with CreateProcess in Visual Studio

后端 未结 3 1467
挽巷
挽巷 2020-12-10 03:09

I\'ve created a Windows/C++/WTL application that spawns a child process. The two processes communicate via anonymous pipes.

I\'d like to be able to debug the child

3条回答
  •  萌比男神i
    2020-12-10 03:30

    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.cpp
    HANDLE hMutex = ::CreateMutex(NULL, TRUE, "Global\\some_guid");
    ::CreateProcess(...);
    ::ReleaseMutex(hMutex); // breakpoint here
    ::CloseHandle(hMutex);
    
    // child.cpp
    int 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.

提交回复
热议问题