Attach to process macro

Recently I got fed up with manually attaching to game process every time. I knew there were some macros that would search for given executable and attach automatically, I even found some of them. Sadly, it seems like API changes slightly with every release, so couldn’t get any of them to work with MSVC2010. In the end, I just hacked my own one, attaching it below (MSVC2010, probably won’t work with any other version).

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Security.Principal
Imports System.Windows.Forms

Public Module DebugHelpers

Sub AttachToExe()
    If Not (AttachToProcess("MY_EXE0.exe")) Then
        If Not AttachToProcess("MY_EXE1.exe") Then
            MessageBox.Show("Can't find EE executable")
        End If
    End If
End Sub

Function AttachToProcess(ByVal ProcessName As String) As Boolean
    Dim processFound = False
    Try
        Dim processes As EnvDTE.Processes = DTE.Debugger.LocalProcesses
        If processes.Count = 0 Then
            MessageBox.Show("No processes are running on this machine.", _
                "Error")
        Else
            For Each proc As EnvDTE.Process In processes
                If proc.Name.IndexOf(ProcessName) >= 0 Then
                    proc.Attach()
                    processFound = True
                    Exit For
                End If
            Next
        End If
    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try

    AttachToProcess = processFound
End Function

End Module

Just create a new macro, customize it to search for executable of your choice, bind it to some key combination (Ctrl+Alt+D for me) and you should be good to go.

Old comments

JF 2012-06-11 21:45:19

Pretty good idea! Sadly, it won’t work in VS2012 since MS removed the macro functionality. Should be able to do it in an extension though.

mike 2012-06-11 22:51:48

It may have been faster to just use the record functionality :).

Jonathan 2012-06-12 10:29:38

An alternative but similar approach is to get the program to request being attached to a debugger. It’s the same thing that happens when the process crashes and the JIT Debugger window pops up only without the crashing part.
You can see an example of a function to do that here:
http://www.codeproject.com/Tips/254524/Self-debugger-attach-to-process
I use this because on our project game server processes are spawned on demand and it’s easier than manually using the Attach to Process window.

admin 2012-06-12 13:35:18

@mike - unless I’m missing something, record would only work if your process is always at the exact same index

Arseny Kapoulkine 2012-06-12 17:28:28

I’ve always just used VS for that. Specify a project path, set “Attach” to true in Debugging tab in project settings. F5 for attach/debug. Probably only works if the executable is in the exact same path.

mike 2012-06-14 14:39:28

Hmm my mistake then. I remembered doing this with record in vs2008, but my memory might fail me and maybe I ended up editing the script anyway.

Ricardo Costa 2012-06-16 19:56:26

I’ve always used this macro from the record functionality and it worked so far. I think it saves the process name and not its index in the list, if you select it by double-clicking on its name when recording.

BinarySplit 2012-06-25 10:29:25

I’ve been using a macro like this for a while for web development. Since the IIS web server process, w3wp.exe, runs continuously, it’s faster than hitting F5 and starting the development server process. I got pretty good at hitting “Alt+T, P, w3, enter, enter” back before I learned about the macros.
Another useful macro is to collapse all files in Solution Explorer: http://blogs.thesitedoctor.co.uk/tim/2010/02/27/Collapse+All+Solution+Explorer+Items+In+Visual+Studio+2010.aspx

More Reading
Older// A Decade