Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug C# BHO project in visual studio/internet explorer

I'm creating an IE extension in C# using visual studio 2010. How do I go about debugging the extension whilst running it in Internet Explorer?

like image 747
opsb Avatar asked Nov 03 '10 12:11

opsb


1 Answers

A few things are very striking:

  1. This question is being asked a lot
  2. Most answers, if not all, are incomplete or incorrect

So here it goes: In VS2010. perform the following:

  1. Create your BHO project, a good starting point is: Demo IE Toolbar/BHO
  2. Create a similar solution/project, Go to "Solution Explorer", Right Click your project or use Alt+Enter and go to "Properties":

Project properties

  1. Be sure the debug profile is selected:

Debug Profile

  1. We will need some post build events to register our assembly:

Defining post build events

These are the different commands:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\gacutil.exe" /u "$(TargetName)"
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\gacutil.exe" /f /i "$(TargetPath)"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" /unregister /codebase "$(TargetPath)"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" /codebase "$(TargetPath)"

The order is important. First the assembly gets unregistered, then registered. The first time you run this, build will fail since these post-build events will fail. This is normal, the first time you build, there was no assembly registered and as such there is nothing to unregister. The second time you build, everything will work just fine. At this stage, after a successful, error-free build, manually starting IE should result in your BHO being visible:

Check BHO registered

Check BHO registered II

  1. Now we would also like to be able to just go and press F5, have the whole thing build, open IE and attach the debugger. Contrary to popular belief however, the VS 2010 debugger will not attach on its own, not even when defining "Start external program" in "Debug" (which in fact, is still necessary):

Start IE external

Doing so will start IE, your BHO should also run but breakpoints will not be hit.

To solve this we will use:

public virtual void SetSite(Object pUnkSite)
    {

#if DEBUG
        Debugger.Launch();
#endif

        ... Other code ...
    }

This ensures that the debugger gets attached early on in the BHO lifecycle. Read about the nitty gritty details here.

Pressing F5 now will result in a few dialogs asking you which debugger to attach:

Debug dialog I

Debug dialog II

Debug Dialog III

From thereon out it' s happy debugging:

Breakpoints can be hit!

I hope this helps!

EDIT

I recently was asked to bring some updates to a rather ancient BHO I wrote. Revisiting my own tutorial, I noticed some issues might come up when following it:

1) After quickly deploying a W7 machine with VS2010 (as released) I got a funky error when an attempt was made to attach the debugger:

Framweork version errors!

I could resolve the issue by also installing VS2010 SP1 (as I used it originally) although I have no clue why this was happening.

2) Right now, when an attempt is made to attach the debugger, the instance of VS2010 holding my project is not in the list of available debuggers.

Debugger not there!

However, when I just cancel all dialogs and restart IE, the running instance is magically there and I can hit my breakpoints once again. The issue seems related to questions by others.

EDIT 2

The 2nd issue was solved after a full reboot, just as in the linked question.

like image 101
Kris Avatar answered Sep 25 '22 15:09

Kris