How to attach a process to the current debugger programmatically?

ε祈祈猫儿з 提交于 2020-08-11 04:04:07

问题


I have 2 projects in the solution that compile to A.exe and B.exe. A will start B and connect to it through a pipe. I want to test the connection between them so I'd like to attach to both at the same time. Doing that manually every time is very inconvenient so I tried this

if(IsDebuggerPresent())
    DebugActiveProcess(processId);

However it seems B is attached to A's debugger instead of Visual Studio's debugger. So how can I attach B to VS debugger automatically?

I have windbg at hand for debugging crash dumps so it may also be a solution, as long as it can be done without multiple user interactions


回答1:


Attach to a process B programatically

Method 1: Using VS

  1. Open B.vcxproj and not your solution (.sln containning projects A and B)
  2. Put your breakpoints

  1. Run this dos command from your project A via system() (or other variant):

    devenv /nosplash /run "G:\Logiciels\B\B.vcxproj" /nosplash /debugexe

  2. devenv is a command in your VS path. In my case it in:

    C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC

  3. When you run this command, B project is opened and break point reached.

Method 2: Using Windbg

Just call this command:

windbg -p ProcessID

Attach to a process B graphically

Open your B project in another instance, and attach to it like this :

And select your B.exe:



来源:https://stackoverflow.com/questions/60541684/how-to-attach-a-process-to-the-current-debugger-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!