2010-07-28 19 views
3

Je reçois cette erreur:RegistryTreeChangeEvent via C# & WMI

Exception non gérée: System.Runtime.InteropServices.COMException (0x80042001): Exception de HRESULT: 0x80042001 à System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal (Int32 errorCode, IntPtr errorInfo) à System.Management.ManagementEventWatcher.Start() à MyNamespace.Program.Main (String [] args) {dans somedir} \ Program.cs: ligne 16

Et voici ma console C# application que j'utilise pour regarder le registre:

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; 
     } 
    } 
} 

}

Je suis en train de regarder simplement le registre des changements. Quelqu'un at-il des suggestions pour expliquer pourquoi cela échoue?

Répondre

4

Il est une erreur WMI interne, WBEMESS_E_REGISTRATION_TOO_BROAD, « L'enregistrement du fournisseur chevauche le domaine des événements système. »

Voilà à peu près un bon message d'erreur, comme vous l'avez déjà fait pour COM. Trouver à quel point les messages d'exception .NET sont meilleurs. Anyhoo, je suis assez sûr que ce que cela signifie est "vous demandez WAY trop d'événements". Vous devrez être plus sélectif dans votre requête, utilisez la clause WHERE. Comme:

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


Poussé par Giorgi, j'ai trouvé le MSDN page qui documente le problème:

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.

+0

Voilà qui est intéressant. Je me demande pourquoi cette réponse a été rejetée. – bitcycle

+0

Revanche downvote. –

+0

En fait, la documentation indique que vous obtiendrez cette erreur sans la clause where. – Giorgi