title: Attaching to a child process automatically in Visual Studio during Debugging
body: <p>When writing plugins for media center your plugin is hosted in <code>ehexthost.exe</code> this exe gets launched from <code>ehshell.exe</code> and you have no way of launching it directly, instead you pass a special param to <code>ehshell.exe</code> which will launch the plugin in a separate process.</p>
  
  <p>When we are debugging <a href="http://code.google.com/p/videobrowser/source/checkout" rel="noreferrer">media browser</a> I find the process of attaching to a second process kind of clunky, I know about Debugger.Attach and also of some <a href="http://msdn.microsoft.com/en-us/library/a329t4ed.aspx" rel="noreferrer">special registry</a> entries I can use.</p>
  
  <p>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?  </p>
  
  <p>If there is a plugin for VS that allows me to achieve this functionality I would be happy with it. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I ended up going with the following macro: </p>
  
  <pre><code>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") &lt;&gt; -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
  </code></pre>
  
  <p>Also, I outlined the process on how to <a href="http://www.samsaffron.com/archive/2009/01/28/Simpler+debugging+of+Vista+Media+Center+plugins" rel="noreferrer">get this going</a> on my blog.  </p>