Subscribe Now: Feed Icon

Tuesday, May 3, 2011

Silverlight: Attach to Process Shortcut

Back when I was a WinForm programmer I used a keyboard shortcut to attach the process of that program to the Visual Studio Debugger, it was a fairly easy process involving recording a macro and attaching it to a keyboard shortcut. This morning I decided to do the same to my IE Silverlight window using this guide but run into a few difficulties:

  1. The way the guide got the computer name just didn’t work, I found out from here that Environment.MachineName works.
  2. It didn’t attach to the Silverlight process – thus didn’t work!

 

    1. Sub NotWorkingAttachToSilverlight()
    2.     Try
    3.         Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
    4.         Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
    5.         Dim dbgeng(2) As EnvDTE80.Engine
    6.         dbgeng(0) = trans.Engines.Item("Silverlight")
    7.         Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, Environment.MachineName).Item("iexplore.exe")
    8.         proc2.Attach2(dbgeng)
    9.     Catch ex As System.Exception
    10.         MsgBox(ex.Message)
    11.     End Try
    12. End Sub

     

    Why didn’t it attach to the Silverlight process?

    Silverlight in IE works by using two processes: one for the window (red outline) and the other for Silverlight(selected):

    VS-attach-to-silverlight

    Now whenever I tested the Macro it just attached itself to the first process!

    The solution was to iterate through the processes until finding the one containing the Silverlight program and attaching to that process:

    1. Sub AttachToSilverlight()
    2.     Try
    3.         Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
    4.         Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
    5.         Dim dbgeng(1) As EnvDTE80.Engine
    6.         dbgeng(0) = trans.Engines.Item("Silverlight")
    7.  
    8.         For Each process As EnvDTE80.Process2 In dbg2.GetProcesses(trans, Environment.MachineName)
    9.             If process.Name.Contains("iexplore.exe") Then
    10.                 For Each program As EnvDTE.Program In process.Programs
    11.                     If program.Name.Contains("Silverlight") Then
    12.                         process.Attach2(dbgeng)
    13.                     End If
    14.                 Next
    15.  
    16.             End If
    17.         Next
    18.  
    19.     Catch ex As System.Exception
    20.         MsgBox(ex.Message)
    21.     End Try
    22. End Sub

    Attach the Macro to a keyboard shortcut is as easy as:

    Keyboard-shortcut

    (Ctrl+Alt+S was for the Server Explorer but I don’t know many people who use this shortcut since the Server Explorer is by default always on).

     

    Keywords: Silverlight, Debugging

    IceRocket Tags: ,