Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / Visual Basic

Just Attach

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
17 Sep 2009CPOL2 min read 8.7K   3  
JustAttach – a macro that finds out the output file path of the startup project of your solution and automatically attaches to it.

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

VB
 1: Imports System
 2: Imports EnvDTE
 3: Imports EnvDTE80
 4: Imports EnvDTE90
 5: Imports System.Diagnostics
 6:
 7: Public Module SenthilMacros
 8:     Public Sub JustAttach()
 9:         Dim solutionBuild As SolutionBuild = DTE.Solution.SolutionBuild
10:         Dim startupProjectName As String = solutionBuild.StartupProjects(0)
11:
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
16:
17:         Dim startupProject As Project = FindProject(startupProjectName.Trim())
18:         Dim outputFilePath As String = FindOutputFileForProject(startupProject)
19:
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
24:
25:         Attach(outputFilePath)
26:     End Sub
27:     Sub Attach(ByVal file As String)
28:         Dim process As EnvDTE.Process
29:
30:         For Each process In DTE.Debugger.LocalProcesses
31:             If process.Name = file Then
32:                 process.Attach()
33:                 Return
34:             End If
35:         Next
36:
37:         MsgBox("Could not attach because " + file + _
                   " is not found in the list of running processes", _
                   MsgBoxStyle.Critical, "Failed to Attach")
38:     End Sub
39:
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()
51:
52:         Dim config As Configuration = _
                project.ConfigurationManager.ActiveConfiguration
53:         Dim buildPath = config.Properties.Item("OutputPath").Value.ToString()
54:
55:         If String.IsNullOrEmpty(fileName) Or String.IsNullOrEmpty(projectPath) Then
56:             Return ""
57:         End If
58:
59:         Dim folderPath As String = System.IO.Path.Combine(projectPath, buildPath)
60:         Return System.IO.Path.Combine(folderPath, fileName)
61:
62:     End Function
63: End Module
This article was originally posted at http://msmvps.com/blogs/senthil/rss.aspx

License

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


Written By
Software Developer Atmel R&D India Pvt. Ltd.
India India
I'm a 27 yrs old developer working with Atmel R&D India Pvt. Ltd., Chennai. I'm currently working in C# and C++, but I've done some Java programming as well. I was a Microsoft MVP in Visual C# from 2007 to 2009.

You can read My Blog here. I've also done some open source software - please visit my website to know more.

Comments and Discussions

 
-- There are no messages in this forum --