如何在 Visual Studio/Internet Explorer 中调试 C# BHO 项目
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事非常引人注目:
所以事情是这样的: 在 VS2010 中。执行以下操作:
这些是不同的命令:
顺序很重要。首先程序集被取消注册,然后被注册。第一次运行此命令时,构建将失败,因为这些构建后事件将失败。这是正常的,第一次构建时,没有注册程序集,因此没有什么可以注销的。第二次构建时,一切都会正常工作。在此阶段,在成功且无错误地构建之后,手动启动 IE 应该会导致您的 BHO 可见:
F5
,构建整个过程,打开 IE 并附加调试器。然而,与普遍看法相反,VS 2010 调试器不会自行附加,即使在“调试”中定义“启动外部程序”时也不会(事实上,这仍然是必要的):这样做将启动 IE,您的 BHO 也应该运行,但不会命中断点。
为了解决这个问题,我们将使用:
这确保了调试器在 BHO 生命周期的早期就被附加。请此处了解详细信息。
现在按
F5
将出现几个对话框,询问您要附加哪个调试器:从那时起,调试就愉快了:
我希望这会有所帮助!
编辑
最近我被要求对我写的一个相当古老的 BHO 进行一些更新。重新审视我自己的教程,我注意到在遵循它时可能会出现一些问题:
1)在使用 VS2010(已发布)快速部署 W7 机器后,当尝试附加调试器时,我遇到了一个奇怪的错误:
< /a>
我可以通过安装 VS2010 SP1(因为我最初使用它)来解决这个问题,尽管我不知道为什么会发生这种情况。
2) 现在,当尝试附加调试器时,保存我的项目的 VS2010 实例不在可用调试器列表中。
但是,当我取消所有对话框并重新启动 IE 时,正在运行的实例就神奇地出现了,我可以再次命中断点。该问题似乎与其他人的问题。
编辑2
第二个问题在完全重新启动后得到解决,就像链接问题。
A few things are very striking:
So here it goes: In VS2010. perform the following:
These are the different commands:
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:
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):Doing so will start IE, your BHO should also run but breakpoints will not be hit.
To solve this we will use:
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:From thereon out it' s happy debugging:
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:
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.
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.
项目+属性,调试选项卡。选择“启动外部程序”,将其设置为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".