24

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?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Bertvan
  • 4,943
  • 5
  • 40
  • 61

6 Answers6

28

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:

Here are other Stack Overflow questions that deal with that:

Community
  • 1
  • 1
George Stocker
  • 57,289
  • 29
  • 176
  • 237
3

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());
Alpesh
  • 606
  • 6
  • 15
3

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

dove
  • 20,469
  • 14
  • 82
  • 108
2

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

Samrat Debroy
  • 396
  • 7
  • 9
2

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.

samoz
  • 56,849
  • 55
  • 141
  • 195
  • WinDbg should be used. WinDbg with SOS really trumps OllyDbg for .NET. OllyDbg has more "analysis" features but WinDbg is more appropriate here -- where the OP probably only wants to debug his own process – kizzx2 Apr 21 '11 at 03:59
0

I use vs 2022 and it's under Debug | Attach to process. (ss attached) enter image description here

Isma Rekathakusuma
  • 856
  • 12
  • 18