📜 ⬆️ ⬇️

Quick Attach to Process

Visual Studio has a convenient opportunity to join a running process (Tools - Attach to Process ...) during debugging. Convenient as long as you do not have to do it with enviable regularity, then choosing the right process from the list that opens becomes extremely tedious. That is why there is a desire to automate these simple actions.

Consider the solution on the example of Visual Studio 2010, for other versions there are no fundamental differences.

1. First, in Macro Explorer (Tools - Macros - Macro Explorer) we will create a new module AttachToProcess.


2. In the module code, add the function to attach to the process with the ProcessName parameter.
Public Function AttachToProcess(ByVal ProcessName As String) As Boolean Dim success As Boolean success = True Try Dim debugger As EnvDTE80.Debugger2 = DTE.Debugger Dim transport As EnvDTE80.Transport = debugger.Transports.Item("Default") Dim name As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name name = name.Substring(0, name.IndexOf("\")) Dim process As EnvDTE80.Process2 = debugger.GetProcesses(transport, name).Item(ProcessName) process.Attach() Catch ex As System.Exception success = False End Try Return success End Function 

3. Add a method that will be used later to call from Visual Studio. It specifies the name of the process.
 Sub AttachToW3WP() If Not AttachToProcess("w3wp.exe") Then System.Windows.Forms.MessageBox.Show("Cannot attach to process") End If End Sub 

The final module code.
 Public Module AttachToProcess Public Function AttachToProcess(ByVal ProcessName As String) As Boolean Dim success As Boolean success = True Try Dim debugger As EnvDTE80.Debugger2 = DTE.Debugger Dim transport As EnvDTE80.Transport = debugger.Transports.Item("Default") Dim name As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name name = name.Substring(0, name.IndexOf("\")) Dim process As EnvDTE80.Process2 = debugger.GetProcesses(transport, name).Item(ProcessName) process.Attach() Catch ex As System.Exception success = False End Try Return success End Function Sub AttachToW3WP() If Not AttachToProcess("w3wp.exe") Then System.Windows.Forms.MessageBox.Show("Cannot attach to process") End If End Sub End Module 

You can run the macro manually and check that everything works as it should.

')
4. Add a toolbar for the macro button (View - Toolbars - Customize ...).



5. Add a button (command). Macros are in the Macros category, surprisingly enough.



Now we have a panel with a button to quickly join the process.


6. The final touch. Assign a key combination to run the macro.


Voila! From now on, you can call Attach to the desired process in one click with the mouse or with the help of hot keys.

Source: https://habr.com/ru/post/131937/


All Articles