Introduction
Here is the macro I found to remotely attach to a process on another machine.
Background
If you are SharePoint's developers, you know all too well how to debug our solutions. If you're building a new Web Part, in order to debug it, you have to manually attach the Visual Studio debugger to the W3SVC.EXE process by selecting Debug -> Attached to Process... and then select one or more instances of the W3SVC.EXE process, and click Attach. This is tedious... and repetitive.
Why we don't set repetitive processes can be automated in Visual Studio? Here, we use a macro to debug any process that we want.
How to Create a Debugging Macro?
If you are coding in Visual Studio, it is very easy to create a new macro and run it as follows:
- Choose Macro Explorer by the following or press Alt+F8:
- Edit My Macros:
- Microsoft Visual Studio Macros will open and you can see:
Add New Item:
- Download the source code and pass it to your new item.
- Create Quick Launch for your macro.
5.1 Select Customize
5.2 Select Keyboard...
5.3 Select your macros:
5.4 Press Shortcut Keys:
5.5 Assign and OK:
- Now, you are ready to use this macro only by pressing "Ctr+Shift+E" instead of selecting Debug -> Attached to Process... and then selecting one or more instances of the W3SVC.EXE process, and click Attach.
Using the Code
In the code, we need imports
like the following:
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
If you want to debug from another machine, you need to run AttachToVirtualMachine()
by entering the domain name, user name and machine name.
Dim domain As String = "YoyrDomain"
Dim username As String = "YourName"
Dim machine As String = "YourMachine"
Specify a process that you want to attach:
Dim processToAttachTo As String = "w3wp.exe"
Here is the entire code of the Macro
method to remote attach to a process on another machine:
Sub AttachToVirtualMachine()
Try
Dim domain As String = "YoyrDomain"
Dim username As String = "YourName"
Dim machine As String = "YourMachine"
Dim processToAttachTo As String = "w3wp.exe"
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(1) As EnvDTE80.Engine
Dim processRemote As EnvDTE80.Process2
Dim fullUsername = domain + "\" + username
Dim transportQualifier = fullUsername + "@" + machine
dbgeng(0) = trans.Engines.Item("Workflow")
Dim processesRemote = dbg2.GetProcesses(trans, transportQualifier)
Dim attached As Boolean
attached = False
For Each processRemote In processesRemote
If (Right(processRemote.Name, Len(processToAttachTo)) = _
processToAttachTo) Then
If processRemote.UserName = fullUsername Then
processRemote.Attach2()
attached = True
End If
End If
Next
If Not attached Then
MsgBox(processToAttachTo & " is not running")
End If
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
And, if you want to debug on your machine, you need the following function:
Function AttachToProcess(ByVal processName As String) As Boolean
Dim proc As EnvDTE.Process
Dim attached As Boolean
For Each proc In DTE.Debugger.LocalProcesses
If (Right(proc.Name, Len(processName)) = processName) Then
proc.Attach()
attached = True
End If
Next
Return attached
End Function
and:
Sub AttachDebuggerToIIS()
Dim processToAttachTo As String = "w3wp.exe"
If Not AttachToProcess(processToAttachTo) Then
MsgBox(processToAttachTo & " is not running")
End If
End Sub
Note
If you want to debug another process such as "OWSTIMER.EXE", you need to change:
Dim processToAttachTo As String = "OWSTIMER.EXE"
At method AttachToVirtualMachine
, you can find a process that runs by another user by entering their name:
Dim attached As Boolean
attached = False
For Each processRemote In processesRemote
If (Right(processRemote.Name, Len(processToAttachTo)) = processToAttachTo) Then
If processRemote.UserName = "Administrators\admin" Then
processRemote.Attach2()
attached = True
End If
End If
Next
Points of Interest
Please see Andrew Connell's blog.
History
- August-18-2009: Version 1.0
My full name is Sần Hải Quang. I'm software engineer of Bamboo Solutions. My company is a Microsoft Gold-certified partner, is a leading global provider of SharePoint Web Parts and technologies that extend the power of the SharePoint platform. Over 4,500 organizations worldwide have chosen to enhance their SharePoint deployment with products and solutions from Bamboo. If you have SharePoint, you need Bamboo!