Click here to Skip to main content
Click here to Skip to main content

Quick Attach to Process by Macro

, 18 Aug 2009
Rate this:
Please Sign up or sign in to vote.
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:

  2. Edit My Macros:

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

    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

    5.2 Select Keyboard...

    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:

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.

'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:

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
        '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:

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

License

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

About the Author

Sần Hải Quang
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

 
GeneralDiscussion Pinmemberhaiquang18-Aug-09 16:53 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Aug 2009
Article Copyright 2009 by Sần Hải Quang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid