Wednesday 15 October 2008

Attach Debugger macro in VS 2005/ 2008

Question:
How to use a macro in Visual Studio 2005/2008 to attach a debugger to a process?



Answer:
We often have to debug processes by attaching to them.
For example, when you do unit testing and you want to see why something went wrong you have to attach your debugger to the test tool's process.
And when you do this a hundred times a day you'll really get bored.

So here is a little macro script that attaches to NUnit or asp_wp or VS WebDev.WebServer (cassini)
If you are on Win 2003 the asp worker process is called w3wp.exe
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Imports System.Security.Principal

Public Module DebuggingJPE
Function AttachToProcess(ByVal processName As String)
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")

Dim dbgeng(1) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("Managed")

'Dim compName As String = WindowsIdentity.GetCurrent().Name
'compName = compName.Substring(0, compName.IndexOf("\"))
Dim computerName As String = Environment.MachineName

Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, computerName).Item(processName)
proc2.Attach2(dbgeng)

Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Function
Sub AttachToAspNET()
AttachToProcess("aspnet_wp.exe")
End Sub
Sub AttachToNUnit()
AttachToProcess("nunit.exe")
End Sub
Sub AttachToWebDev()
AttachToProcess("WebDev.WebServer.EXE")
End Sub


End Module

No comments: