How to debug C# BHO project in visual studio/inter

2019-01-24 16:59发布

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?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-24 17:05

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.

查看更多
We Are One
3楼-- · 2019-01-24 17:20

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".

查看更多
登录 后发表回答