WMIEvent 类列表

发布于 2024-07-25 06:13:07 字数 175 浏览 6 评论 0原文

最近在学习WMI和WQL。 我找到了可以查询的 Win32 类列表(来自 MSDN),但我无法找到事件类列表(应该是 Win32 类列表的子集,不是吗?)有一个清单或某种备忘单吗? 我只是出于好奇才问这个问题。

事件类示例 - Win32_ProcessStartTrace

Recently I have been learning about WMI and WQL. I found out the list of Win32 classes (from MSDN) that I can query for but I am not able to find out the list of event classes (should be the subset of the list of Win32 classes isn't it ?) Does any one have a list or some kind of cheat sheet for this? I am jsut asking this out of curiosity.

Example for an event class - Win32_ProcessStartTrace

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

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

发布评论

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

评论(3

时光瘦了 2024-08-01 06:13:07

以下是如何使用 C# 和 System.Management 列出 root\cimv2 命名空间中的 WMI 事件类:

using System;
using System.Management;

class Program
{
    static void Main()
    {
        string query =
            @"Select * From Meta_Class Where __This Isa '__Event'";

        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(query);

        foreach (ManagementBaseObject cimv2Class in searcher.Get())
        {
            Console.WriteLine(cimv2Class.ClassPath.ClassName);
        }
    }
}

root\cimv2 是默认的 WMI 命名空间,因此您不必使用 ManagementScope 实例。 传递给 ManagementObjectSearcher 的 WQL 查询是 WMI 元数据查询。 它使用:

  • Meta_Class 将查询指定为架构查询,并使用
  • __This 属性递归列出 __Event 子类

(请参阅此处此处)。

如果 WMI 类的提供程序实现为事件 WMI 提供程序,则 WMI 类是事件类,并且必须是 __Event 的子类。 这并不意味着您不能在 WQL 事件查询中使用“普通”WMI 类,例如 Win32_ProcessWin32_Service。 您只需使用 __InstanceOperationEvent 派生帮助器类之一,例如 __InstanceCreationEvent__InstanceDeletionEvent,WMI 将使用其自己的事件子系统来传递事件。

以下是订阅 Win32_Process 创建事件的示例 WQL 查询:

Select * From __InstanceCreationEvent Within 5 Where TargetInstance Isa 'Win32_Process'

在这种情况下,您必须使用 Within 子句。

Here's how to list WMI event classes in the root\cimv2 namespace with C# and System.Management:

using System;
using System.Management;

class Program
{
    static void Main()
    {
        string query =
            @"Select * From Meta_Class Where __This Isa '__Event'";

        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher(query);

        foreach (ManagementBaseObject cimv2Class in searcher.Get())
        {
            Console.WriteLine(cimv2Class.ClassPath.ClassName);
        }
    }
}

root\cimv2 is the default WMI namespace so you don't have to use a ManagementScope instance. The WQL query passed to ManagementObjectSearcher is a WMI metadata query. It uses:

  • Meta_Class to designate the query as a schema query, and
  • __This property to recursively list __Event subclasses

(see here and here).

WMI class is an event class if its provider implemented as an event WMI provider and must be a subclass of __Event. This doesn't mean that you can't use 'ordinary' WMI classes like Win32_Process and Win32_Service in WQL event queries. You just have to use one of the __InstanceOperationEvent derived helper classes like __InstanceCreationEvent or __InstanceDeletionEvent, and WMI will use its own event subsystem to deliver events.

Here is a sample WQL query that subscribes to Win32_Process creation events:

Select * From __InstanceCreationEvent Within 5 Where TargetInstance Isa 'Win32_Process'

In this case you have to use the Within clause.

夕色琉璃 2024-08-01 06:13:07

WMI 代码创建器是学习 WMI 的一个很好的工具,除其他外,它可以让您探索本地或远程计算机上的 WMI 事件类并生成用于接收事件通知的代码。

编辑:由于您将问题标记为C#,您可能对以编程方式获取从特定类派生的事件类列表的代码感兴趣:

using System.Management;
...

string ancestor = "WMIEvent";     // the ancestor class
string scope = "root\\wmi";       // the WMI namespace to search within

try
{
    EnumerationOptions options = new EnumerationOptions();
    options.ReturnImmediately = true;
    options.Rewindable = false;

    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher(scope, "SELECT * FROM meta_class", options);

    foreach (ManagementClass cls in searcher.Get())
    {
        if (cls.Derivation.Contains(ancestor))
        {
            Console.WriteLine(cls["__CLASS"].ToString());
        }
    }
}
catch (ManagementException exception)
{
    Console.WriteLine(exception.Message);
}

WMI Code Creator is a great tool for learning WMI that, among other things, lets you explore WMI event classes on the local or remote computer and generate code for receiving event notifications.

Edit: Since you tagged your question as C#, you might be interested in the code for getting the list of event classes derived from a particular class programmatically:

using System.Management;
...

string ancestor = "WMIEvent";     // the ancestor class
string scope = "root\\wmi";       // the WMI namespace to search within

try
{
    EnumerationOptions options = new EnumerationOptions();
    options.ReturnImmediately = true;
    options.Rewindable = false;

    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher(scope, "SELECT * FROM meta_class", options);

    foreach (ManagementClass cls in searcher.Get())
    {
        if (cls.Derivation.Contains(ancestor))
        {
            Console.WriteLine(cls["__CLASS"].ToString());
        }
    }
}
catch (ManagementException exception)
{
    Console.WriteLine(exception.Message);
}
莫相离 2024-08-01 06:13:07

MSDN 没有列出所有 MSMCA 类吗

更新:
我没有使用 WMI 进行大量工作,但我刚刚发现了这个 WMI 工具 会很有帮助。 它为您提供了一个用于查看对象的 WMI 层次结构的 GUI,甚至允许您注册和使用事件。 这应该会为您提供所需的信息。

Doesn't MSDN have a list of all the MSMCA classes here

UPDATE:
I don't do tons of work with WMI, but I just found this WMI tool that would have been helpful. It gives you a GUI for viewing the WMI hierarchy of objects, and even allows you to register and consume events. This should give you the information you need.

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