Just Attach

Attach to what? To the process you want to debug, of course.

How many developers attach to and debug arbitrary processes running on their machines? Very few, I’d imagine. And I’d think that even those few people typically prefer Windbg or an equivalent debugger to the one supplied with Visual Studio.

Which means that aside from ASP .NET developers, almost all of us using Visual Studio attach to the application that we are working on and nothing else. To attach

1. You summon the “Attach to Process” dialog by hitting Ctrl + Alt + P. Or if you are a mouse person, you go to Debug –> Attach to Process.

2. You search for your application in the list of processes shown and select it.

3. You optionally change some settings and then hit OK.

The second step can be particularly annoying, especially if the name of your application starts with a particularly common letter that occurs in the latter half of the English alphabet (it’s a tie between ‘s’ and ‘v’ on my machine). Even otherwise, if you’ve worked on the application for a significant amount of time, the keystrokes to select it becomes part of muscle memory (down arrow, down arrow, Enter, for e.g.,), and you occasionally end up attaching to the wrong application because some other process sneaked in. Surely there must be a better way?

Enter JustAttach – a macro that does just that. It finds out the output file path of the startup project of your solution and automatically attaches to it.

The full macro code is at the end of the blog post. You can also download, unzip, open Macro Explorer (View –> Other Windows –> Macro Explorer) and select Load Macro Project to start using it right away.

Do your fingers a favor by binding the command to a VS shortcut (Tools –> Options –> Keyboard, type JustAttach in the textbox and choose a shortcut like Ctrl + Alt+ Y) ; your fingers will thank you for it :).

   1: Imports System

   2: Imports EnvDTE

   3: Imports EnvDTE80

   4: Imports EnvDTE90

   5: Imports System.Diagnostics


   7: Public Module SenthilMacros

   8:     Public Sub JustAttach()

   9:         Dim solutionBuild As SolutionBuild = DTE.Solution.SolutionBuild

  10:         Dim startupProjectName As String = solutionBuild.StartupProjects(0)


  12:         If String.IsNullOrEmpty(startupProjectName) Then

  13:             MsgBox("Could not attach because the startup project could not be determined", MsgBoxStyle.Critical, "Failed to Attach")

  14:             Return

  15:         End If


  17:         Dim startupProject As Project = FindProject(startupProjectName.Trim())

  18:         Dim outputFilePath As String = FindOutputFileForProject(startupProject)


  20:         If String.IsNullOrEmpty(outputFilePath) Then

  21:             MsgBox("Could not attach because output file path for the startup project could not be determined", MsgBoxStyle.Critical, "Failed to Attach")

  22:             Return

  23:         End If


  25:         Attach(outputFilePath)

  26:     End Sub

  27:     Sub Attach(ByVal file As String)

  28:         Dim process As EnvDTE.Process


  30:         For Each process In DTE.Debugger.LocalProcesses

  31:             If process.Name = file Then

  32:                 process.Attach()

  33:                 Return

  34:             End If

  35:         Next


  37:         MsgBox("Could not attach because " + file + " is not found in the list of running processes", MsgBoxStyle.Critical, "Failed to Attach")

  38:     End Sub


  40:     Function FindProject(ByVal projectName As String) As Project

  41:         Dim project As Project

  42:         For Each project In DTE.Solution.Projects

  43:             If project.UniqueName = projectName Then

  44:                 Return project

  45:             End If

  46:         Next

  47:     End Function

  48:     Function FindOutputFileForProject(ByVal project As Project) As String

  49:         Dim fileName As String = project.Properties.Item("OutputFileName").Value.ToString()

  50:         Dim projectPath As String = project.Properties.Item("LocalPath").Value.ToString()


  52:         Dim config As Configuration = project.ConfigurationManager.ActiveConfiguration

  53:         Dim buildPath = config.Properties.Item("OutputPath").Value.ToString()


  55:         If String.IsNullOrEmpty(fileName) Or String.IsNullOrEmpty(projectPath) Then

  56:             Return ""

  57:         End If


  59:         Dim folderPath As String = System.IO.Path.Combine(projectPath, buildPath)

  60:         Return System.IO.Path.Combine(folderPath, fileName)


  62:     End Function

  63: End Module


2 thoughts on “Just Attach”

  1. Thanks a lot for sharing this info. I just love this. I have been working on Windows App and I was tired of using C+ A+ P and selecting the app name. This one is so easy. My entire team also loving it. We have around 25 dev in our team and it is a great help.


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>