COM 异常在 MMC 管理单元中接收 WMI 事件

发布于 2024-08-11 05:21:10 字数 734 浏览 2 评论 0原文

我正在尝试创建一个 MMC 管理单元,除其他外,该管理单元将接收并显示从我的服务触发的日志事件。我已经创建了观察程序并且它正确触发,但是当我尝试将节点添加到 MmcListView 时,我收到一个 COMException,其中显示“无法使用已与其底层 RCW 分离的 COM 对象”。

下面是有问题的代码:

private void LogEvent_EventArrived(object sender, EventArrivedEventArgs e)
{
    LogEvent logEvent = new LogEvent(e.NewEvent);
    if (SnapIn.InvokeRequired)
    {
        object[] args = new object[] { logEvent };
        SnapIn.Invoke(new ManagementAction(AddEvent), args);
    }
    else
        AddEvent(logEvent);
}

LogEvent 类是一个简单的类,它负责将 ManagmentBaseObject 属性转换为易于编码的类型安全属性。 AddEvent 方法执行将 LogEvent 对象添加到 MmcListView 的 ResultNodes 列表的实际工作。

我提到的异常实际上是在第二个事件被触发时调用的,并且似乎是在调用 InvokeRequired 时发生的。我不知道这里到底发生了什么。有什么想法吗?

I am attempting to create an MMC snapin that among other things, will receive and display log events that are fired from my service. I have created the watcher and it fires correctly, but when I try to add nodes to my MmcListView, I am getting a COMException which says "COM object that has been separated from its underlying RCW cannot be used."

Here is the code in question:

private void LogEvent_EventArrived(object sender, EventArrivedEventArgs e)
{
    LogEvent logEvent = new LogEvent(e.NewEvent);
    if (SnapIn.InvokeRequired)
    {
        object[] args = new object[] { logEvent };
        SnapIn.Invoke(new ManagementAction(AddEvent), args);
    }
    else
        AddEvent(logEvent);
}

The LogEvent class is a simple class which does the work of converting the ManagmentBaseObject properties to type safe properties which are easily coded against. The AddEvent method does the actual work of adding the LogEvent object to the ResultNodes list of the MmcListView.

The Exception I mentioned is actually called on the SECOND event being fired, and seems to happen on the call to InvokeRequired. I have no idea what is actually going on here. Any ideas?

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

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

发布评论

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

评论(1

残疾 2024-08-18 05:21:10

好吧,这太愚蠢了……但实际情况是,我通过 Invoke 调用的方法抛出了异常,并且它没有按调用者的预期冒泡,而是显示为奇怪的 COM 异常。一旦我清除了通过委托调用的方法中的异常,一切就正常了。

OK, this was incredibly stupid... but the reality was that the method I was calling via Invoke was throwing an exception, and it wasn't bubbling up as expected to the caller, and instead was showing as the weird COM exception. Once I cleared up the exception in the method I was calling via delegate, everything worked fine.

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