Monitor registry changes (VBMonitorRegistryChange)

The code sample demonstrates how to monitor registry change events in VB

VB.NET (51.7 KB)
9,171 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • xp sp3 vs2010
    2 Posts | Last post March 22, 2013
    • Why on xp sp3 and vs2010 VBMonitorRegistryChange crashes with error 'Invalid class'?
      Some help.
    • I found this was because the ManagementScope isn't set properly on the watcher.  The constructor for the watcher should look something like this:
          Public Sub New(ByVal hive As RegistryKey, ByVal keyPath As String)
              Me.Hive = hive
              Me.KeyPath = keyPath
              ' If you set the platform of this project to x86 and run it on a 64bit 
              ' machine, you will get the Registry Key under 
              ' HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node when the key path is
              Me.KeyToMonitor = hive.OpenSubKey(keyPath)
              If KeyToMonitor IsNot Nothing Then
                  ' Construct the query string.
                  Dim queryString As String = String.Format("SELECT * FROM RegistryKeyChangeEvent " & ControlChars.CrLf & "                   WHERE Hive = '{0}' AND KeyPath = '{1}' ", Me.Hive.Name, Me.KeyPath)
                  ' Construct the scope string.
                  Dim scopePath As String = "\\.\root\default"
                  Dim scope As ManagementScope = New ManagementScope(scopePath)
                  Dim query As New WqlEventQuery()
                  query.QueryString = queryString
                  query.EventClassName = "RegistryKeyChangeEvent"
                  query.WithinInterval = New TimeSpan(0, 0, 0, 1)
                  Me.Query = query
                  Me.Scope = scope
                  AddHandler EventArrived, AddressOf RegistryWatcher_EventArrived
                  Dim message As String = String.Format("The registry key {0}\{1} does not exist", hive.Name, keyPath)
                  Throw New ArgumentException(message)
              End If
          End Sub
  • Can't get it to work
    2 Posts | Last post March 22, 2013
    • Get a "Cross-Threading" error message on Main Form, line 142:
    • The error occurs because adding a list item to a control by just calling "Items.Add" isn't a thread safe operation.
      Here's an updated function with the original call remarked out, the "AddToItemList" and it's callback are added to provide thread safe calls to update that text box:
          ''' <summary>
          ''' Handle the RegistryKeyChangeEvent.
          ''' </summary>
          Private Sub watcher_RegistryKeyChangeEvent(ByVal sender As Object, ByVal e As RegistryKeyChangeEventArgs)
              Dim newEventMessage As String = String.Format(
                  "{0} The key {1}\{2} changed",
              AddToItemList(lstChanges, newEventMessage)
          End Sub
          Delegate Sub AddToItemListCallback(ByVal control As Control, ByVal message As String)
          Private Sub AddToItemList(ByVal control As Control, ByVal message As String)
              If control.InvokeRequired Then
                  Dim atmlCallback As New AddToItemListCallback(AddressOf AddToItemList)
                  control.Invoke(atmlCallback, New Object() {control, message})
              End If
          End Sub