How do I attach a process to the debugger in Visual Studio?

我们两清 提交于 2019-12-28 11:45:28

问题


I know I can start a process in code with Process.Start(). Is it also possible to attach the debugger to that process?

Not from code per se , but just a way to do it?


回答1:


You can attach to a running process using Tools | Attach to Process. If it's a Web Application, you can attach to it by attaching to aspnet_wp.exe or w3wp.exe.

To answer your question on how to attach to a process programmatically:

  • Attaching to a Process Using VS.NET Automation Model

Here are other Stack Overflow questions that deal with that:

  • Communicating with the Visual Studio Debugger Programmatically?
  • Programmatically apply breakpoints in Visual Studio



回答2:


In visual studio click Tools | Attach to process. Then select appropriate service.




回答3:


You can do this in pretty much any debugger worth its salt.

Visual Studio has one that should fit your needs.

If you need a little more advanced control, try OllyDbg, which is a disassembler, so you can actually manipulate your program at the assembly level. This will give you complete control, but it might be information overload as well.




回答4:


In Visual Studio 2015, click 'Debug > Attach to process' in the menu. Alternatively, there is a shortcut key Ctrl+Alt+P.




回答5:


You can do this in your code.

public static void Attach(DTE2 dte)
        {
            var processes = dte.Debugger.LocalProcesses;
            foreach (var proc in processes.Cast<EnvDTE.Process>().Where(proc => proc.Name.IndexOf("YourProcess.exe") != -1))
                proc.Attach();
        }

        internal static DTE2 GetCurrent()
        {
            var dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.12.0"); // For VisualStudio 2013

            return dte2;
        }

Usage:

Attach(GetCurrent());


来源:https://stackoverflow.com/questions/986470/how-do-i-attach-a-process-to-the-debugger-in-visual-studio

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