如何在 Visual Studio/Internet Explorer 中调试 C# BHO 项目

发布于 2024-09-30 14:13:54 字数 82 浏览 3 评论 0原文

我正在使用 Visual Studio 2010 在 C# 中创建 IE 扩展。在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-10-07 14:13:54

有几件事非常引人注目:

  1. 这个问题被问了很多次
  2. 大多数答案(如果不是全部的话)都是不完整或不正确的

所以事情是这样的: 在 VS2010 中。执行以下操作:

  1. 创建 BHO 项目,一个好的起点是: 演示 IE 工具栏/BHO
  2. 创建一个类似的解决方案/项目,转到“解决方案资源管理器”,右键单击您的项目或使用 Alt+Enter 并转到“属性”:

项目属性

  1. 确保选择调试配置文件:

调试配置文件

  1. 我们需要一些构建后事件来注册我们的程序集:

Defining post build events

这些是不同的命令:

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

顺序很重要。首先程序集被取消注册,然后被注册。第一次运行此命令时,构建将失败,因为这些构建后事件将失败。这是正常的,第一次构建时,没有注册程序集,因此没有什么可以注销的。第二次构建时,一切都会正常工作。在此阶段,在成功且无错误地构建之后,手动启动 IE 应该会导致您的 BHO 可见:

Check BHO Registration

检查 BHO 注册 II

  1. 现在我们还希望能够直接按 F5 ,构建整个过程,打开 IE 并附加调试器。然而,与普遍看法相反,VS 2010 调试器不会自行附加,即使在“调试”中定义“启动外部程序”时也不会(事实上,这仍然是必要的):

Start IE external

这样做将启动 IE,您的 BHO 也应该运行,但不会命中断点。

为了解决这个问题,我们将使用:

public virtual void SetSite(Object pUnkSite)
    {

#if DEBUG
        Debugger.Launch();
#endif

        ... Other code ...
    }

这确保了调试器在 BHO 生命周期的早期就被附加。请此处了解详细信息。

现在按 F5 将出现几个对话框,询问您要附加哪个调试器:

调试对话框 I

调试对话框 II

调试对话框 III

从那时起,调试就愉快了:

可以命中断点!

我希望这会有所帮助!

编辑

最近我被要求对我写的一个相当古老的 BHO 进行一些更新。重新审视我自己的教程,我注意到在遵循它时可能会出现一些问题:

1)在使用 VS2010(已发布)快速部署 W7 机器后,当尝试附加调试器时,我遇到了一个奇怪的错误:

Framweork 版本错误!< /a>

我可以通过安装 VS2010 SP1(因为我最初使用它)来解决这个问题,尽管我不知道为什么会发生这种情况。

2) 现在,当尝试附加调试器时,保存我的项目的 VS2010 实例不在可用调试器列表中。

调试器不那里!

但是,当我取消所有对话框并重新启动 IE 时,正在运行的实例就神奇地出现了,我可以再次命中断点。该问题似乎与其他人的问题。

编辑2

第二个问题在完全重新启动后得到解决,就像链接问题

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.

雨后彩虹 2024-10-07 14:13:54

项目+属性,调试选项卡。选择“启动外部程序”,将其设置为c:\program files\internet explorer\iexplore.exe。您可能希望将“命令行参数”设置为执行 BHO 的 HTML 文件的路径。

在要调试的代码上设置断点。当您按 F5 时,Internet Explorer 将开始运行。您会看到断点变成空心,表明该断点未布防。一旦 IE 加载您的 DLL(在“输出”窗口中可见),它就会变成纯红色。当 IE 调用您的代码时,调试器会自动中断。

有一个注册步骤。请始终避免使用 gacutil.exe,它只会污染计算机上的 GAC。始终支持 IDE 中的“注册 COM 互操作”选项,相当于使用 /codebase 选项运行 Regasm.exe。这样就不需要 GAC 了。无论哪种方式,VS 都必须以提升的方式运行才能进行这些计算机配置更改,通过右键单击快捷方式并选择“以管理员身份运行”来启动它。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文