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:
- The way the guide got the computer name just didn’t work, I found out from here that Environment.MachineName works.
- It didn’t attach to the Silverlight process – thus didn’t work!
- Sub NotWorkingAttachToSilverlight()
- Try
- Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
- Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
- Dim dbgeng(2) As EnvDTE80.Engine
- dbgeng(0) = trans.Engines.Item("Silverlight")
- Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, Environment.MachineName).Item("iexplore.exe")
- proc2.Attach2(dbgeng)
- Catch ex As System.Exception
- MsgBox(ex.Message)
- End Try
- 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):
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:
- Sub AttachToSilverlight()
- 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("Silverlight")
- For Each process As EnvDTE80.Process2 In dbg2.GetProcesses(trans, Environment.MachineName)
- If process.Name.Contains("iexplore.exe") Then
- For Each program As EnvDTE.Program In process.Programs
- If program.Name.Contains("Silverlight") Then
- process.Attach2(dbgeng)
- End If
- Next
- End If
- Next
- Catch ex As System.Exception
- MsgBox(ex.Message)
- End Try
- End Sub
Attach the Macro to a keyboard shortcut is as easy as:
(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