Managed WMI Event 类不是事件类吗?

发布于 2024-10-10 01:19:50 字数 2004 浏览 0 评论 0原文

我正在使用此处的说明: http://msdn.microsoft .com/en-us/library/ms257351(VS.80).aspx 创建托管事件类。这是我编写的代码:

[ManagementEntity]
[InstrumentationClass(InstrumentationType.Event)]
public class MyEvent
{
    [ManagementKey]
    public string ID { get; set; }
    [ManagementEnumerator]
    static public IEnumerable<MyEvent> EnumerateInstances()
    {
        var e = new MyEvent() { ID = "9A3C1B7E-8F3E-4C54-8030-B0169DE922C6" };
        return new MyEvent[] { e };
    }
}

class Program
{
    static void Main(string[] args)
    {
        var thisAssembly = typeof(Program).Assembly;
        var wmi_installer = new AssemblyInstaller(thisAssembly, null);
        wmi_installer.Install(null);
        wmi_installer.Commit(null);

        InstrumentationManager.RegisterAssembly(thisAssembly);

        Console.Write("Press Enter...");
        Console.ReadLine();
        var e = new MyEvent() { ID = "A6144A9E-0667-415B-9903-220652AB7334" };
        Instrumentation.Fire(e);

        Console.Write("Press Enter...");
        Console.ReadLine();
        wmi_installer.Uninstall(null);
    }

}

我可以运行一个程序,并且它可以正确安装。使用 wbemtest.exe 我可以浏览到该事件,然后“show mof”:

[dynamic: ToInstance, provider("WmiTest, 
      Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
class MyEvent
{
    [read, key] string ID;
};

注意,该类不是从 __ExtrinsicEvent 继承的,这很奇怪......

我还可以运行 select * from MyEvent,并获取结果Instrumentation.Fire() 也不会返回任何错误。但是,当我尝试使用“通知查询”选项 订阅事件时,我收到 0x80041059

编号:0x80041059

设施:WMI

描述:类不是事件类。

我做错了什么,是否有正确的方法来创建托管 WMI 事件?

I am using directions from here: http://msdn.microsoft.com/en-us/library/ms257351(VS.80).aspx to create a managed event class. Here's the code that I wrote:

[ManagementEntity]
[InstrumentationClass(InstrumentationType.Event)]
public class MyEvent
{
    [ManagementKey]
    public string ID { get; set; }
    [ManagementEnumerator]
    static public IEnumerable<MyEvent> EnumerateInstances()
    {
        var e = new MyEvent() { ID = "9A3C1B7E-8F3E-4C54-8030-B0169DE922C6" };
        return new MyEvent[] { e };
    }
}

class Program
{
    static void Main(string[] args)
    {
        var thisAssembly = typeof(Program).Assembly;
        var wmi_installer = new AssemblyInstaller(thisAssembly, null);
        wmi_installer.Install(null);
        wmi_installer.Commit(null);

        InstrumentationManager.RegisterAssembly(thisAssembly);

        Console.Write("Press Enter...");
        Console.ReadLine();
        var e = new MyEvent() { ID = "A6144A9E-0667-415B-9903-220652AB7334" };
        Instrumentation.Fire(e);

        Console.Write("Press Enter...");
        Console.ReadLine();
        wmi_installer.Uninstall(null);
    }

}

I can run a program, and it properly installs. Using wbemtest.exe I can browse to the event, and "show mof":

[dynamic: ToInstance, provider("WmiTest, 
      Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")]
class MyEvent
{
    [read, key] string ID;
};

Notice, the class does not inherit from __ExtrinsicEvent, which is weird...

I can also run select * from MyEvent, and get the result. Instrumentation.Fire() also returns no error. However, when I'm trying to subscribe to event using "Notification Query" option, I'm getting 0x80041059

Number: 0x80041059

Facility: WMI

Description: Class is not an event class.

What am I doing wrong, and is there a correct way to create managed WMI event?

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

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

发布评论

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

评论(1

本宫微胖 2024-10-17 01:19:50

经过一番挖掘,我弄清楚了发生了什么:显然在框架 4 中,引入了 WMI 类和属性的第二个“分支”,这与经典的类和属性发生了干扰。我在互联网上找到的所有示例代码都是在考虑到 .NET 2.0 WMI 支持的情况下编写的。我还没有找到使用 .NET 4 类从 __Event 或 __ExtrinsicEvent 继承类的方法。

令人非常恼火的是,微软在同一个命名空间中引入了两个不兼容的代码分支,它们不仅不能相互兼容,而且还破坏了彼此的功能。

无论如何,为了解决这个问题,我需要做的基本上是确保我的应用程序使用 .NET 2 代码:

  • 完全摆脱 wmi 程序集中的 DefaultManagementInstaller 派生类。使用DefaultManagementProjectInstaller。
  • 使用 Instrumentation.RegisterAssembly 而不是 InstrumentationManager.RegisterAssembly
  • Uninstall() 上对 WMI 命名空间进行一些手动清理,因为 wmi 类未从其中正确删除.NET 2 api 的命名空间。
  • 应对不可能使用 .NET 2 api 将字段设为 [key] 的事实

After some digging, I figured out what happened: apparently in framework 4, there has been a second "branch" of WMI classes and attributes introduced, which interferes with classic ones. All the sample code I found on the internet is written with .NET 2.0 WMI support in mind. I have not found a way with .NET 4 classes to inherit a class from __Event or __ExtrinsicEvent.

It has been very annoying to find that Microsoft introduced two incompatible branches of code into the same namespace, which not only don't work with each other, but they break each other's functionality.

At any rate, what I needed to do in order to fix the issue was essentially making sure my app is using .NET 2 code:

  • completely get rid of DefaultManagementInstaller derived class inside wmi assembly. Use DefaultManagementProjectInstaller.
  • use Instrumentation.RegisterAssembly instead of InstrumentationManager.RegisterAssembly
  • do some manual cleanup of WMI namespace on Uninstall(), since wmi classes are not properly removed from the namespace with .NET 2 api.
  • cope with the fact that it is impossible to make a field to be a [key] with .NET 2 api
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文