Create a one-step operation to attach to a process

If you find your debugging session typically involves attaching to a specific process you can save yourself some time in VS 2005 by creating a macro to do it for you.

  1. Choose Tools->Macros->Record Temporary Macro
  2. Choose Tools->Attach to Process and choose the process you want to attach to.  You can choose any combination of process, transport, qualifier and code-type.
  3. Click the Stop Recording macro toolbar button, or press Ctrl-Shift-R.
  4. Choose Tools->Macros->Save Temporary Macro.  The caret will be moved to the Macro Explorer and you'll be prompted to change the name of TemporaryMacro to the name of your choosing. 

Here's an example macro that was generated when I attached to calc.exe with the native engine:

Sub AttachToCalc()
           Try

                  Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger

                  Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")

                  Dim dbgeng(1) As EnvDTE80.Engine

                  dbgeng(0) = trans.Engines.Item("Native")

                  Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "JIMSMACHINE").Item("calc.exe")

                  proc2.Attach2(dbgeng)

 

           Catch ex As System.Exception

                  MsgBox(ex.Message)

           End Try

 

End Sub

 

Now, invoking this macro through the Macro Explorer will cause the exact same attach to whatever program you originally chose!  If you want to make your life even easier you could add this macro to a toolbar by:

  1. Choose Tools->Customize
  2. Activate the Commands Tab.
  3. Scroll down to "Macros" in the Categories list.
  4. Drag Macros.MyMacros.RecordingModule.AttachToCalc to a toolbar

You could also create an alias for the macro that you can invoke from the command window as follows:

 

   alias attcalc Macros.MyMacros.RecordingModule.AttachToCalc

 

Now to attach to calc, all you have to do is type "attcalc" in the command window.

 

Of course, the macro that is created for you is a very simple skeleton.  If you'd like you could modify it to do all sorts of other things, like attach to additional processes, or set a breakpoint or whatever! 

 

You might also want to peruse my Idiot's Guide to Creating and Using VS Macros for more helpful hints.