views:

905

answers:

3

I'm normally developing web apps, and a surprisingly large amount of my work time is spent doing "Ctrl + Alt + P", sorting by Process Name, and picking w3wp.exe to attach my debugger.

To make matters worse, I'm working on an app that spans several application pools, so I normally have 2 or 3 instances of w3wp.exe, and it's impossible to know which one to attach to, so I normally end up attaching to all of them, which is overkill but works.

All in all, this is pretty annoying...

My colleague figured out a way to have a VS Macro to automatically attach to w3wp.exe (he basically recorded this):

Sub AttachMacro()    
  Try    
    Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger    
    Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")    
    Dim dbgeng(3) As EnvDTE80.Engine    
    dbgeng(0) = trans.Engines.Item("T-SQL")    
    dbgeng(1) = trans.Engines.Item("T-SQL")    
    dbgeng(2) = trans.Engines.Item("Managed")    
    Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "ENIAC").Item("w3wp.exe")    
    proc2.Attach2(dbgeng)    
  Catch ex As System.Exception    
    MsgBox(ex.Message)    
  End Try    
End Sub

I'm not really sure whether all that is necessary, or anything, I have never made a macro for VS, I don't really know where to start.

Would there be a way to modify this macro so that instead of attaching itself to an instance of w3wp.exe, it will attach itself to all instances of w3wp.exe?

A: 

You may want to check out gflags.exe. One of its options is a debugger to run attached to every invocation of a particular executable.

Logan Capaldo
I'm not really sure that'll work...What is that going to do? Open a new instance of Visual Studio every time w3wp.exe runs?I need several w3wp.exe processes attached to the same VS instance, and I need it to be THE instance where I have my project loaded...
Daniel Magliola
+5  A: 
Sub MacroAttachToAllProcesses()

  Try

   Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
   Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
   Dim dbgeng(3) As EnvDTE80.Engine

   dbgeng(0) = trans.Engines.Item("T-SQL")
   dbgeng(1) = trans.Engines.Item("T-SQL")
   dbgeng(2) = trans.Engines.Item("Managed")

    For Each theProcess As EnvDTE80.Process2 In dbg2.GetProcesses(trans, "COMPUTERNAME")
      If theProcess.Name.Contains("w3wp.exe") Then
      theProcess.Attach2(dbgeng)
    End If

  Next

   Catch ex As System.Exception
    MsgBox(ex.Message)
   End Try

End Sub
DanC
Take it even further and assign it to a toolbar... scroll to the bottom of this post: http://www.milkcarton.com/blog/2007/05/20/Using+Visual+Studio+Macros+To+Increase+Productivity.aspx
Mike Atlas
Seems the url for this blog has changed so thought is worth updating so you dont have to find it like me. http://www.milkcarton.com/blog/default,month,2007-05.aspx
Cragly
+1  A: 

I know that you are looking for a Macro for this task, and I have similar macros. However I would like to explain a way to attach the debugger to the projects in your solution when you start to debug.

It's a little known feature - if you right-click on your solution file in the solution browser, Choose properties, you can then define multiple startup projects and their action. Your debugger will attach to the listed projects when you run it.

Note: If you have a web-service it will open a browser window, however you can disable that in the properties of the project by telling it not to open a window.

Brian Schmitt