简体   繁体   English

RegistryTreeChangeEvent通过C#和WMI

[英]RegistryTreeChangeEvent via C# & WMI

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 未处理的异常:System.Runtime.InteropServices.COMException(0x80042001):来自HRESULT的异常:0x80042001位于System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo),位于MyNamespace.Program的System.Management.ManagementEventWatcher.Start()处{somedir} \\ Program.cs中的.Main(String [] args):第16行

And here's my C# console app that I'm using to watch the registry: 这是我用来观看注册表的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 trying to simply watch the registry for changes. 我试图只是看注册表的变化。 Does anyone have any suggestions as to why this is failing? 有没有人有任何关于为什么失败的建议?

It is an internal WMI error, WBEMESS_E_REGISTRATION_TOO_BROAD, "The provider registration overlaps with the system event domain." 这是内部WMI错误WBEMESS_E_REGISTRATION_TOO_BROAD,“提供程序注册与系统事件域重叠”。

That's about a good an error message as you'd ever get out of COM. 这是一个很好的错误信息,因为你已经离开了COM。 Striking how much better the .NET exception messages are. 突出.NET异常消息的好坏程度。 Anyhoo, I'm fairly sure that what it means is "you are asking for WAY too many events". Anyhoo,我很确定这意味着“你要求太多事件”。 You'll have to be more selective in your query, use the WHERE clause. 您必须在查询中更具选择性,使用WHERE子句。 Like: 喜欢:

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


Prompted by Giorgi, I found the MSDN page that documents the problem: 在Giorgi的推动下,我找到了记录问题的MSDN页面

The following is an example of an incorrect registration. 以下是错误注册的示例。

SELECT * FROM RegistryTreeChangeEvent WHERE hive = hkey_local_machine" OR rootpath ="software" 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. 由于无法评估每个属性的可能值,因此WMI拒绝错误WBEM_E_TOO_BROAD任何没有WHERE子句的查询,或者WHERE子句太宽而无法使用。

As Hans has told you are receiving the error because you haven't specified where clause. 正如汉斯告诉你的那样,你收到了错误,因为你没有指定where子句。 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. 根据为Registry Provider创建适当的WHERE子句,您必须指定where子句,否则您将收到WBEM_E_TOO_BROAD错误。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM