通过C#和RegistryTreeChangeEvent WMI

发布于 2024-09-12 12:32:15 字数 1506 浏览 3 评论 0原文

我收到此错误:

未处理的异常:System.Runtime.InteropServices.COMException(0x80042001):来自 HRESULT 的异常:0x80042001 在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo) 在 System.Management.ManagementEventWatcher.Start() at MyNamespace.Program.Main(String[] args) in {somedir}\Program.cs:line 16

这是我用来监视注册表的 C# 控制台应用程序:

using System;
using System.Management;

namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {

        var watcher = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM RegistryTreeChangeEvent"));
        var handler = new MyHandler();
        watcher.EventArrived += handler.Arrived;

        //Start watching for events
        watcher.Start();

        while (handler.EventHasntFiredYet)
        {
            // Nothing.
        }

        //Stop watching
        watcher.Stop();
    }

    public class MyHandler
    {
        public bool EventHasntFiredYet;

        public MyHandler()
        {
            EventHasntFiredYet = true;
        }

        public void Arrived(object sender, EventArrivedEventArgs e)
        {
            var propertyDataCollection = e.NewEvent.Properties;
            foreach (var p in propertyDataCollection)
            {
                Console.WriteLine("{0} -- {1}",p.Name,p.Value);
            }
            EventHasntFiredYet = false;
        }
    }
}

}

我试图简单地监视注册表变化。有人对为什么失败有任何建议吗?

I'm getting this error:

Unhandled Exception: System.Runtime.InteropServices.COMException (0x80042001): Exception from HRESULT: 0x80042001
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementEventWatcher.Start()
at MyNamespace.Program.Main(String[] args) in {somedir}\Program.cs:line 16

And here's my C# console app that I'm using to watch the registry:

using System;
using System.Management;

namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {

        var watcher = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM RegistryTreeChangeEvent"));
        var handler = new MyHandler();
        watcher.EventArrived += handler.Arrived;

        //Start watching for events
        watcher.Start();

        while (handler.EventHasntFiredYet)
        {
            // Nothing.
        }

        //Stop watching
        watcher.Stop();
    }

    public class MyHandler
    {
        public bool EventHasntFiredYet;

        public MyHandler()
        {
            EventHasntFiredYet = true;
        }

        public void Arrived(object sender, EventArrivedEventArgs e)
        {
            var propertyDataCollection = e.NewEvent.Properties;
            foreach (var p in propertyDataCollection)
            {
                Console.WriteLine("{0} -- {1}",p.Name,p.Value);
            }
            EventHasntFiredYet = false;
        }
    }
}

}

I'm trying to simply watch the registry for changes. Does anyone have any suggestions as to why this is failing?

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

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

发布评论

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

评论(2

帅冕 2024-09-19 12:32:25

正如 Hans 所说,您收到错误是因为您没有指定 where 子句。根据 为注册表提供程序创建正确的 WHERE 子句< /a> 您必须指定 where 子句,否则您将收到 WBEM_E_TOO_BROAD 错误。

为了简化代码而不是重新发明轮子,您可以使用以下库: 异步注册表通知在 .NET 中使用强类型 WMI 类

As Hans has told you are receiving the error because you haven't specified where clause. According to Creating a Proper WHERE Clause for the Registry Provider you must specify where clause otherwise you will receive WBEM_E_TOO_BROAD error.

To simplify your code and not to reinvent the wheel you can use the following library: Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET

三寸金莲 2024-09-19 12:32:23

这是一个内部 WMI 错误,WBEMESS_E_REGISTRATION_TOO_BROAD,“提供程序注册与系统事件域重叠。”

这是您从 COM 中得到的一条很好的错误消息。令人震惊的是 .NET 异常消息有多好。不管怎样,我相当确定这意味着“你要求的事件太多了”。您必须在查询中更有选择性,使用 WHERE 子句。喜欢:

从RegistryTreeChangeEvent中选择*
WHERE Hive='HKEY_LOCAL_MACHINE' 和
'RootPath='软件\微软'


在 Giorgi 的提示下,我找到了 MSDN 页面 记录了该问题:

以下是错误注册的示例。

从RegistryTreeChangeEvent中选择*
WHERE hive = hkey_local_machine"
或者根路径=“软件”

由于无法评估每个属性的可能值,因此 WMI 会拒绝任何没有 WHERE 子句或 WHERE 子句太宽泛而无法使用的查询,并返回错误 WBEM_E_TOO_BROAD。< /p>

It is an internal WMI error, WBEMESS_E_REGISTRATION_TOO_BROAD, "The provider registration overlaps with the system event domain."

That's about a good an error message as you'd ever get out of COM. Striking how much better the .NET exception messages are. Anyhoo, I'm fairly sure that what it means is "you are asking for WAY too many events". You'll have to be more selective in your query, use the WHERE clause. Like:

SELECT * FROM RegistryTreeChangeEvent
WHERE Hive='HKEY_LOCAL_MACHINE' AND
'RootPath='SOFTWARE\Microsoft'


Prompted by Giorgi, I found the MSDN page that documents the problem:

The following is an example of an incorrect registration.

SELECT * FROM RegistryTreeChangeEvent
WHERE hive = hkey_local_machine"
OR rootpath ="software"

Because there is no way to evaluate the possible values for each of the properties, WMI rejects with the error WBEM_E_TOO_BROAD any query that either does not have a WHERE clause or if the WHERE clause is too broad to be of any use.

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