简体   繁体   English

调用COM-dll组件后,调试器不会捕获C#异常

[英]After calling a COM-dll component, C# exceptions are not caught by the debugger

I'm using a COM dll provided to me by 3rd-party software company (I don't have the source code). 我正在使用由第三方软件公司提供的COM dll(我没有源代码)。 I do know for sure they used Java to implement it because their objects contain property names like 'JvmVersion'. 我确实知道他们使用Java来实现它,因为它们的对象包含诸如“ JvmVersion”之类的属性名称。

After I instantiated an object introduced by the provided COM dll, all exceptions in my C# program cannot be caught by the VS debugger and every time an exception occurs I get the default Windows Debugger Selection dialog (And that's while executing my program in debug mode under a full VisualStudio debugging environment). 实例化由提供的COM dll引入的对象后,VS调试器无法捕获C#程序中的所有异常,并且每次发生异常时,我都会得到默认的Windows调试器选择对话框(并且这是在调试模式下执行程序的情况下,完整的VisualStudio调试环境)。

To illustrate: 为了显示:

throw new Exception("exception 1");
m_moo = new moo();   // Component taken from the COM-dll
throw new Exception("exception 2");

Exception 1 will be caught by VS and show the "yellow exception window". VS将捕获异常1,并显示“黄色异常窗口”。

Exception 2 will open a dialog titled "Visual Studio Just-In-Time Debugger" containing the text "An unhandled win32 exception occurred in myfile.vshost.exe[1348]." 异常2将打开一个标题为“ Visual Studio即时调试器”的对话框,其中包含文本“ myfile.vshost.exe [1348]中发生未处理的win32异常”。 followed by a list of the existing VS instances on my system to select from. 然后是我系统上现有VS实例的列表以供选择。

I guess the instantiation of "moo" object overrides C#'s exception handler or something like that. 我猜想“ moo”对象的实例化会覆盖C#的异常处理程序或类似的东西。 Am I correct and is there a way to preserve C#'s exception handler? 我是否正确,有没有办法保留C#的异常处理程序?

Well, that's not good. 好吧,那不是很好。 You however haven't proven yet that the CLR's exception handling logic is completely borked. 但是,您尚未证明CLR的异常处理逻辑是完全令人厌烦的。 What you describe could also be explained by something detaching the debugger. 您描述的内容也可以通过使调试器脱离来解释。 The Java JVM would certainly be a candidate. Java JVM肯定是候选对象。 Check if a try still works, if it does you still stand a chance to use this COM server. 检查是否仍然可以尝试,如果仍然可以使用此COM服务器,则可以尝试。

Debugging is however going to be painful. 但是调试会很痛苦。 With some luck, the detach happens only once. 幸运的是,分离仅发生一次。 Try writing System.Diagnostics.Debugger.Break() after the very first call to the server, click "Debug" on the dialog you'll get. 首次调用服务器后,尝试编写System.Diagnostics.Debugger.Break(),在出现的对话框中单击“调试”。

Also test if a null reference exception still works, that's a hardware exception that's liable to be redirected by a VM calling Windows' SetUnhandledExceptionFilter(). 还测试空引用异常是否仍然有效,这是硬件异常,VM可以通过调用Windows的SetUnhandledExceptionFilter()来重定向该异常。 If that can't be caught then you should not use this COM server as-is, it will destabilize your process too much. 如果无法捕获到该错误,则您不应按原样使用此COM服务器,否则将严重破坏您的进程。 Running it in a separate process and talking to it with WCF or Remoting is a desperation move that could work out. 在一个单独的过程中运行它,并与WCF或Remoting进行交谈是一个绝望的举动,可以解决。

Needless to say, this component vendor is violating the COM server contract badly. 不用说,该组件供应商严重违反了COM服务器合同。 You shouldn't have to put up with it. 您不必忍受它。

Adding the following line (as the first line) in my main() (in Program.cs file) made exceptions work again: 在main()(在Program.cs文件中)中添加以下行(作为第一行)使异常再次起作用:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

The above line overides the Automatic handler which (I guess) for some reason stopped working after using the COM component. 上面的代码行覆盖了自动处理程序,该处理程序在我使用COM组件后由于某种原因而停止工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM