Auto-attaching to aspnet_wp.exe

, ,

Dennis van der Stelt asked me how to debug CSModules without using the Community Server SDK. Ok, so here’s how I debug my modules.

First I set the output directory of my projects to CS’ bin folder. To debug the module, I attach the debugger manually to ASP.NET worker process, aspnet_wp.exe. However, that’s not very ergonomic, because you have to go to DebugAttach To Process, select aspnet_wp.exe (fortunately, processes are sorted by their name) and click OK.

However, after a while that gets really annoying.

Therefore I searched for a simpler solution, e.g. an add-in. I stumbled over this nice and short macro, published by Roy Osherove (who else? [;)]):

This was on the win-tech-off-topic mailing list:

A macro to automatically attach to aspnet_wp.exe, written by Kevin Dente can save lots of clicking around time:

Sub AttachAspNet()
    Dim process As EnvDTE.Process
    If Not (DTE.Debugger.DebuggedProcesses Is Nothing) Then
        For Each process In DTE.Debugger.DebuggedProcesses
            If (process.Name.IndexOf("aspnet\_wp.exe") \<\> -1) Then
                Exit Sub
            End If
        Next
    End If
    For Each process In DTE.Debugger.LocalProcesses
        If (process.Name.IndexOf("aspnet\_wp.exe") \<\> -1) Then
            process.Attach()
            Exit Sub
        End If
    Next
End Sub

Unfortunately, it’s not perfect. Process.Attach doesn’t let you specify the program type (CLR, Script, native, etc). I think that it uses whatever your last selection was in the UI. But don’t quote me on that, it’s been a while.

I added the macro to a toolbar, so debugging my modules is only one click far. [:)]

Comments

Anonymous

Great tip! I also use this macro.

And this is my modification of the macro:

Public Module AttachToAspNet
Declare Function SetForegroundWindow Lib “user32” (ByVal hwnd As Long) As Long

Declare Function keybd_event Lib “user32” (ByVal bVk As Byte, ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long) As Long

Const VK_F5 = &H74

Function RefreshIE(ByVal ifRefresh As Boolean)
Dim process As System.Diagnostics.Process
Dim processes As System.Diagnostics.Process()
processes = System.Diagnostics.Process.GetProcesses()
For Each process In processes
If (process.ProcessName = “iexplore”) Then
SetForegroundWindow(process.MainWindowHandle.ToInt32())
If ifRefresh Then
keybd_event(VK_F5, 0, 0, 0)
End If
Exit Function
End If
Next
End Function

Function GetAspNetWpProcessID() As Integer
Dim process As System.Diagnostics.Process
Dim processes As System.Diagnostics.Process()
processes = System.Diagnostics.Process.GetProcesses()
For Each process In processes
If (process.ProcessName = “aspnet_wp”) Then
Return process.Id
End If
Next
Return -1
End Function

Sub AttachToAspNet()
Dim process As EnvDTE.Process

Dim aspnet_wpProcessID As Integer
aspnet_wpProcessID = GetAspNetWpProcessID()
If aspnet_wpProcessID = -1 Then
Exit Sub
End If

For Each process In DTE.Debugger.DebuggedProcesses
If (process.ProcessID = aspnet_wpProcessID) Then
Exit Sub
End If
Next

For Each process In DTE.Debugger.LocalProcesses
If (process.ProcessID = aspnet_wpProcessID) Then
process.Attach()
If MsgBox(“Refresh IE?”, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
RefreshIE(True)
Exit Sub
End If
End If
Next
End Sub
End Module

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...