且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

运行COM DLL引发的异常

更新时间:2021-12-27 08:01:21

您唯一明显的问题是您没有问题。您使用的COM服务器是用托管语言编写的。非常简单,仅使用属性即可完成。考虑到您已经问过的问题,可能是C#。关键是这些异常是 silent ,它们被抛出并捕获在托管代码中。

The only obvious problem you have is that you don't have a problem. The COM server you are using was written in a managed language. Pretty common, dead-simple to do with just an attribute. Probably C#, given that you've asked questions about it. The point is that these exceptions are silent, they are thrown and caught inside the managed code.

跨COM边界传递异常是非法的, CLR为您提供坚决的保证,以确保这种情况永远不会发生。如果未捕获到托管异常,则它将变为指示失败的HRESULT错误代码。由于您使用的是#import生成的包装程序,因此您将永远不会真正看到这些错误代码,因此包装程序会抛出 _com_error 将它们转换为C ++异常。您不会尝试捕获它们,因此,如果发生这种情况,您的程序将崩溃并在Terminate()上死亡,不可能不注意到这一点。

Passing exceptions across a COM boundary is illegal, the CLR gives you a rock-hard guarantee that this will never happen. If a managed exception is not caught then it gets turned into an HRESULT error code that indicates a failure. Since you are using the wrappers generated by #import, you'll never actually see these error codes, the wrapper turns them back into a C++ exception by throwing a _com_error. You make no attempt at catching them so if it happens then your program will keel over and die on terminate(), impossible to not notice that.

Fwiw,0x04242420是一个非致命异常代码,是调试器的实现细节。 在此处记录

Fwiw, 0x04242420 is a non-fatal exception code and is an implementation detail of the debugger. Documented here.

EEFileLoadException是内部使用的非托管异常CLR,并在要求Fusion加载程序集且找不到它时触发。变成托管的FileLoadException。鉴于托管的COM服务器通常在查找依赖程序集时遇到问题,因此您可能会稍微担心它。但这通常在托管代码使用XML序列化时发生,我们知道您正在使用它。使用异常进行流控制不是很好,但是性能也是一项功能。关于它的更多信息在这篇文章中

The EEFileLoadException is an unmanaged exception used inside the CLR and triggered when Fusion is asked to load assembly and it can't find it. Turns into a managed FileLoadException. You might be slightly more concerned about it given that managed COM servers often have a problem finding dependent assemblies. But this commonly happens when the managed code uses XML serialization, we know you are using it. Using exceptions for flow-control isn't very nice but performance is a feature as well. More about it in this post.

所以***不为此担心。如果您不相信自己的COM服务器能够正确实施,则只需调试两者即可。项目>属性>调试,并将调试器类型设置从自动更改为混合。并使用调试> Windows>异常设置,然后选中 CLR异常复选框,以在引发托管异常时强制调试器中断。

So rather best to not fret about it. If you don't trust your COM server to be implemented correctly then simply debug both. Project > Properties > Debugging and change the Debugger Type setting from Auto to Mixed. And use Debug > Windows > Exception Settings and tick the checkbox for CLR Exceptions to force the debugger to break when a managed exception is thrown.