36

When writing plugins for media center your plugin is hosted in ehexthost.exe this exe gets launched from ehshell.exe and you have no way of launching it directly, instead you pass a special param to ehshell.exe which will launch the plugin in a separate process.

When we are debugging media browser I find the process of attaching to a second process kind of clunky, I know about Debugger.Attach and also of some special registry entries I can use.

Both these methods don't exactly fit my bill. What I want is to press F5 and have my current instance of visual studio attach to the child process automatically. Can this be done?

If there is a plugin for VS that allows me to achieve this functionality I would be happy with it.

EDIT

I ended up going with the following macro:

Public Sub CompileRunAndAttachToEhExtHost()

    DTE.Solution.SolutionBuild.Build(True)
    DTE.Solution.SolutionBuild.Debug()

    Dim trd As System.Threading.Thread = New System.Threading.Thread(AddressOf AttachToEhExtHost)
    trd.Start()

End Sub

Public Sub AttachToEhExtHost()
    Dim i As Integer = 0
    Do Until i = 50
        i = i + 1
        Try

            For Each proc As EnvDTE.Process In DTE.Debugger.LocalProcesses
                If (proc.Name.IndexOf("ehexthost.exe") <> -1) Then
                    proc.Attach()
                    Exit Sub
                End If
            Next
        Catch e As Exception
            ' dont care - stuff may be busy 
        End Try
        Threading.Thread.Sleep(100)
    Loop
End Sub

Also, I outlined the process on how to get this going on my blog.

Robert
  • 1,286
  • 1
  • 17
  • 37
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • 1
    there is other way with the new Debugger2 interface, http://www.codeplex.com/lazy/SourceControl/changeset/view/20095#307770 – Pablo Retyk Jan 28 '09 at 07:15
  • Is there an advantage to using the Debugger2 interface? – Sam Saffron Jan 28 '09 at 22:34
  • yes, instead of looping on DTE.Debugger.LocalProcesses you can just get the process proc = debugger2.GetProcesses(trans, "").Item(processName) – Pablo Retyk Jan 29 '09 at 11:40
  • My VB is pretty rusty - I get "Name 'proc' is not declared" - any ideas on what needs fixed? – Tim May 10 '11 at 15:08
  • Figured it out. Should be the following: For Each proc As EnvDTE.Process In DTE.Debugger.LocalProcesses – Tim May 10 '11 at 15:12

5 Answers5

38

I would use a macro. I've redefined my F5 function to attach to the asp.net process instead of the long build/validate it usually performs. This works pretty well for me and it's really easy.

    For Each process In DTE.Debugger.LocalProcesses
        If (process.Name.IndexOf("aspnet_wp.exe") <> -1) Then
            process.Attach()
            Exit Sub
        End If
    Next
Jab
  • 13,713
  • 2
  • 42
  • 48
  • Thanks man, I used this to attach to WebDev.WebServer and IE when I hit F4 – Allen Rice Jul 23 '09 at 15:25
  • 2
    any suggestions how to do this in VS2012 now that they've removed macros? – mutex Jul 09 '13 at 03:26
  • You actually no longer have to! At least not for what I needed it for. They added a new option for web projects to "Don't open a page. Wait for a request from an external application." Under the project -> web settings. I'm not sure how I would do it if I needed to attach to a non-web process though. Sorry! – Jab Jul 16 '13 at 20:12
14

For VS2012, macros have been dropped, but you can still do it quite quickly with standard keyboard shortcuts. For instance, to attach to iisexpress.exe:

Ctrl + Alt + p - brings up the Attach To Process dialog

i - jumps to the the first process beginning with i in the list (for me this is iisexpress.exe)

Enter - attaches

For super speed, you can also Turn off Visual Studio Attach security warning when debugging IIS.

Community
  • 1
  • 1
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158
11

Check out the VisualStudio plugin that I wrote, named Lazy.

ANeves
  • 6,219
  • 3
  • 39
  • 63
Pablo Retyk
  • 5,690
  • 6
  • 44
  • 59
  • thanks, I wrote it when I have a similar scenario like yours, my process was invoked by other process and I needed to debug the initialization of my process so I needed to attach it as soon as it starts. If you have any questions let me know. "It works in my machine" – Pablo Retyk Jan 27 '09 at 11:36
  • 1
    After I installed it, it only shows up in VS2008, not VS2010. – Andreas Haferburg Jan 21 '13 at 14:08
  • Hey @pablito, do you have any plans to port your plugin to VS2010/2012+? – Bora Jun 13 '14 at 11:18
  • haven't updated it for a long time, but since some people seems interested I will port it soon to vs2010/12. – Pablo Retyk Jul 01 '14 at 14:34
2

I was debugging a C++ plugin in an externally spawned process that crashed by throwing an exception at startup and this worked perfectly for me:

Add the free Reattach Extension for Visual Studio. Ask it to reattach to the process name before it is launched. It will pop a modal dialog saying it is waiting for the process name to launch.

Now launch the process and the Visual Studio debugger will attach immediately, catching exceptions and hitting breakpoints.

(This was also in a media plugin, the exception was normally caught and rethrown by the host process in a Delphi context so I needed to break before that happened).

mstrange
  • 609
  • 7
  • 13
0

You can automatically attach to a process by pressing F5, if you setup something like that in visual studio:

http://vvcap.net/db/ujYL7zeN_n_RgeprqCSM.htp

notice: There's "Command" filled up as an executable name, and "Attach" must be "yes"

galets
  • 17,802
  • 19
  • 72
  • 101
  • Thanks! But this trick does not work for C# projects you do not have an option like that in the debug list, only the option to start an external program – Sam Saffron Jan 08 '09 at 02:04
  • I think i know what you mean... you can create a "dummy" c++ project and add it to your solution. As long as it attaches top a correct executable, you are good to debug. – galets Jan 13 '09 at 17:36