简体   繁体   English

如何在 Visual Studio 中将进程附加到调试器?

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

I know I can start a process in code with Process.Start() .我知道我可以使用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?不是来自代码本身,而是一种实现方式?

You can attach to a running process using Tools | Attach to Process 您可以使用Tools | Attach to Process 附加到正在运行的进程 Tools | Attach to Process . Tools | Attach to Process If it's a Web Application, you can attach to it by attaching to aspnet_wp.exe or w3wp.exe . 如果它是Web应用程序,您可以通过附加到aspnet_wp.exew3wp.exe来附加它。

To answer your question on how to attach to a process programmatically: 要回答有关如何以编程方式附加到流程的问题:

Here are other Stack Overflow questions that deal with that: 以下是其他Stack Overflow问题:

In visual studio click Tools | 在visual studio中单击工具| Attach to process. 附加到流程。 Then select appropriate service. 然后选择适当的服务。

You can do this in pretty much any debugger worth its salt. 你可以在几乎任何有价值的调试器中执行此操作。

Visual Studio has one that should fit your needs. Visual Studio有一个适合您的需求。

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. 如果您需要更高级的控制,请尝试OllyDbg ,它是一个反汇编程序,因此您可以在程序集级别实际操作程序。 This will give you complete control, but it might be information overload as well. 这将为您提供完全控制,但也可能是信息过载。

In Visual Studio 2015, click 'Debug > Attach to process' in the menu. 在Visual Studio 2015中,单击菜单中的“调试>附加到进程”。 Alternatively, there is a shortcut key Ctrl+Alt+P. 或者,有一个快捷键Ctrl + Alt + P.

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());

I use vs 2022 and it's under Debug |我使用 vs 2022,它在 Debug | Attach to process.附加到进程。 (ss attached) (附加SS) 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2012加载项 - 如何将调试器附加到进程 - Visual studio 2012 add-in - how to attach debugger to a process 我能否以编程方式将正在运行的 .NET 6 进程附加到正在运行的 Visual Studio 调试器实例? - Can I programmatically attach a running .NET 6 process to a running instance of Visual Studio debugger? 如何使Visual Studio宏将调试器附加到w3wp.exe的所有实例? - How can I make a Visual Studio Macro to attach the debugger to all instances of w3wp.exe? 如何在Visual Studio 2008调试器中查看GUID的值? - How do I see the value of a GUID in the Visual Studio 2008 debugger? 如何在创建进程时附加调试器? - How to attach a debugger at process creation? 附加用于在Visual Studio中进行调试的过程 - Attach a process for debugging in Visual Studio 如何在Visual Studio 2010中调试带有附加到进程的build.exe - How to debug a build .exe with attach to process in visual studio 2010 Visual Studio附加处理和查看中断的代码? - Visual Studio attach to process and view the code on break? 如何以编程方式将Visual Studio加载项窗口附加到选项卡? - How do I attach my Visual Studio Add-in window to a tab programmatically? 如何将跟踪侦听器附加到正在运行的进程? - How do I attach a trace listener to a running process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM