调试在Visual Studio中使用CreateProcess生成的进程 [英] debugging a process spawned with CreateProcess in Visual Studio
问题描述
我创建了一个产生子进程的Windows / C ++ / WTL应用程序。这两个进程通过匿名管道进行通信。
我想能够调试子进程。
由于父项目和子项目都在Visual Studio 2008中的同一个解决方案中,有没有办法告诉VS2008我想让调试器调试这两个进程?
当我使用父进程启动调试器时,调试器不会在子进程代码中的任何断点处中断。
由于子进程是由父进程产生的,我不能想象一个附加子进程(可能通过VS2008的另一个实例)的简单方法。
$ b $您可以将一个全局命名的互斥体放在 CreateProcess
调用,然后尝试在子进程中捕获互斥体。如果你然后在 CreateProcess
调用放置一个断点,你应该有时间附加到孩子,在它做任何大量的。
请注意,您会错过子进程中 main
之前发生的任何事情。
一个例子,这样的东西,未经测试:
// parent.cpp
HANDLE hMutex = :: CreateMutex ,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);
...
}
用 #if _DEBUG
或环境变量检查来换行。
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 process.
Since both parent and child projects are in the same solution in Visual Studio 2008, is there any way to tell VS2008 that I'd like the debugger to debug both processes?
When I start the debugger with the parent process, the debugger won't break on any breakpoints in the child process code.
And since the child process is spawned by the parent process, I can't think of an easy way of attaching the child process (maybe via another instance of VS2008) when it's spawned.
Any insights greatly appreciated!
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.
这篇关于调试在Visual Studio中使用CreateProcess生成的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!