Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic

Quick Attach to Process by Macro

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
18 Aug 2009CPOL2 min read 43.4K   510   17   2
Quick Attach to Process by Macro

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:

  1. Choose Macro Explorer by the following or press Alt+F8:

    Image 1

  2. Edit My Macros:

    Image 2

  3. Microsoft Visual Studio Macros will open and you can see:

    Image 3

    Add New Item:

    Click to enlarge image

  4. Download the source code and pass it to your new item.
  5. Create Quick Launch for your macro.

    5.1 Select Customize

    Image 5

    5.2 Select Keyboard...

    Image 6

    5.3 Select your macros:

    Click to enlarge image

    5.4 Press Shortcut Keys:

    Click to enlarge image

    5.5 Assign and OK:

    Click to enlarge image

  6. 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:

VB.NET
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.

VB.NET
'Format with YoyrDomain\YourName@YourMachine
Dim domain As String = "YoyrDomain"
Dim username As String = "YourName"
Dim machine As String = "YourMachine"

Specify a process that you want to attach:

VB.NET
Dim processToAttachTo As String = "w3wp.exe" 

Here is the entire code of the Macro method to remote attach to a process on another machine:

VB.NET
Sub AttachToVirtualMachine()
    Try
        'Format with YoyrDomain\YourName@YourMachine
        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:

VB.NET
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:

VB.NET
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:

VB.NET
Dim processToAttachTo As String = "OWSTIMER.EXE" 

At method AttachToVirtualMachine, you can find a process that runs by another user by entering their name:

VB.NET
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Bamboo Solutions
Vietnam Vietnam
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!

Comments and Discussions

 
QuestionNot able to connect my VM Pin
waveyoke26-Sep-17 23:07
waveyoke26-Sep-17 23:07 
GeneralDiscussion Pin
Sần Hải Quang18-Aug-09 16:53
Sần Hải Quang18-Aug-09 16:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.