来自COM事件处理程序的错误通知,使用互操作 [英] Error notification from COM event handler, using interop

查看:51
本文介绍了来自COM事件处理程序的错误通知,使用互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用com-interop在c#中使用com对象.我为对象提供了一个事件处理程序,该对象在完成其操作后将被调用.这是问题所在.我的处理程序中有一些健全性检查代码,如果世界上一切都不好,则会抛出异常:

I'm consuming a com object in c#, using com-interop. I provide an event handler to the object, which it calls after doing its thing. Here's the problem. I have some sanity-check code in the handler that throws an exception if all is not well with the world:

_comObj.OnRequestCompleted += (int requestID, RequestStatus status) =>
{
    if (<sanity check fails>)
        throw new Exception("This is insane!!!");

    ...
}

com对象显然吞没了这些异常,因为我从未在我的应用程序中看到它们.我该怎么做才能浮出水面"这些异常,以便在异常发生时得到通知?

The com object is apparently swallowing those exceptions, because I never see them in my app. What do I need to do to "surface" those exceptions so that I am notified when they occur?

推荐答案

如果该事件由COM服务器引发,则这不是意外行为.不允许例外跨越COM鸿沟,没有针对例外的二进制互操作标准.CLR在其互操作层中安装了一个包罗万象的异常处理程序,该代码调用了事件处理程序.您引发的异常将转换为HRESULT,这是COM代码报告故障的标准方式.通用Exception对象的值为0x80131500.

This is not unexpected behavior if the event is raised by the COM server. Exceptions are not allowed to cross the COM divide, there is no binary interop standard for exceptions. The CLR installs a catch-all exception handler in its interop layer, the code that calls your event handler. The exception you raised is translated to an HRESULT, the standard way in which COM code reports failure. Which will be 0x80131500 for a generic Exception object.

接下来发生的事情完全取决于COM服务器,它需要对所获得的HRESULT做一些有用的事情.没有看到它做任何事情并不是出乎意料的,函数调用返回值通常被忽略,尤其是在通知样式调用中.当然,您不会得到任何类似于托管异常的东西.

What happens next is entirely up to the COM server, it needs to do something useful with the HRESULT it got. Not seeing it do anything it all is not terribly unexpected, function call return values are often ignored, especially so with notification style calls. You certainly won't get anything resembling a managed exception.

与COM组件供应商或作者合作,以解决可能的问题.情况并非如此.除了记录故障之外,您无能为力.如果这是一个严重的问题,可能无法忽略,请调用Environment.Exit().这与使程序因未处理的异常终止而没什么不同.

Work with the COM component vendor or author to work something out, if possible. Which isn't very often the case. Not much you can do beyond just logging the failure then. Call Environment.Exit() if this is a gross problem that cannot possibly be ignored. This isn't really any different from having your program terminated with an unhandled exception.

这篇关于来自COM事件处理程序的错误通知,使用互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆