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

2

5

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?

Nutmeg answered 3/11, 2010 at 12:10 Comment(0)
I
3

Project + Properties, Debug tab. Select "Start external program", set it to c:\program files\internet explorer\iexplore.exe. You probably want to set the "Command line arguments" to the path of an HTML file that exercises your BHO.

Set a breakpoint on the code you want to debug. Internet Explorer will start running when you press F5. You'll see the breakpoint turning hollow, indicating that the breakpoint is not armed. As soon as IE loads your DLL, visible in the Output window, it will turn into a solid red. And the debugger automatically breaks when IE calls your code.

There is a registration step. Do always avoid using gacutil.exe, it does nothing but pollute the GAC on your machine. Always favor the "Register for COM interop" option in the IDE, the equivalent of running Regasm.exe with the /codebase option. No need for the GAC that way. Either way, VS must be running elevated to make these machine config changes, start it by right-clicking the shortcut and selecting "Run as Administrator".

Idealize answered 3/11, 2010 at 12:26 Comment(4)
This runs internet explorer but my BHO isn't loaded up. Is there some configuration I need to do?Nutmeg
Erm, probably, you have to tell IE to use the BHO. Have you done that?Idealize
I'm working on it :) hadn't realised visual studio wouldn't manage that for me. Looks like I need to use regasm. Unfortunately, it doesn't seem to be available at the command prompt so I'm trying to find out how to do it inside visual studio.Nutmeg
Well, start a new question about it ;)Idealize
A
20

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.

Ambrose answered 13/6, 2014 at 13:49 Comment(4)
Thanks :) I was faced with this problem some time ago and it struck me how little (correct) info was available. Glad it could help!Ambrose
Hmm, I don't see any of those dialogs after pressing F5... all I get is one that says "iexplore.exe has triggered a breakpoint" and asks me to break or continue. Any guesses?Trimmer
@Pauld'Aoust: I don't think lack of dialogs is a problem, as long as you hit the breakpoint.Xantho
@Krumia: I eventually figured it out, but unfortunately I can't remember how, so I can't really contribute my discoveries to the discussion :/Trimmer
I
3

Project + Properties, Debug tab. Select "Start external program", set it to c:\program files\internet explorer\iexplore.exe. You probably want to set the "Command line arguments" to the path of an HTML file that exercises your BHO.

Set a breakpoint on the code you want to debug. Internet Explorer will start running when you press F5. You'll see the breakpoint turning hollow, indicating that the breakpoint is not armed. As soon as IE loads your DLL, visible in the Output window, it will turn into a solid red. And the debugger automatically breaks when IE calls your code.

There is a registration step. Do always avoid using gacutil.exe, it does nothing but pollute the GAC on your machine. Always favor the "Register for COM interop" option in the IDE, the equivalent of running Regasm.exe with the /codebase option. No need for the GAC that way. Either way, VS must be running elevated to make these machine config changes, start it by right-clicking the shortcut and selecting "Run as Administrator".

Idealize answered 3/11, 2010 at 12:26 Comment(4)
This runs internet explorer but my BHO isn't loaded up. Is there some configuration I need to do?Nutmeg
Erm, probably, you have to tell IE to use the BHO. Have you done that?Idealize
I'm working on it :) hadn't realised visual studio wouldn't manage that for me. Looks like I need to use regasm. Unfortunately, it doesn't seem to be available at the command prompt so I'm trying to find out how to do it inside visual studio.Nutmeg
Well, start a new question about it ;)Idealize

© 2022 - 2024 — McMap. All rights reserved.