Notification de modification de la base de registre

Soyez le premier à donner votre avis sur cette source.

Vue 12 787 fois - Téléchargée 824 fois

Description

Salut a tous,

Voici un petit exemple (.net2) qui montre comment etre averit de la modification d'une cle de la bdr

Je n'ai mis ici que le code de notification, pour savoir quelle valeur a ete modifiee supprimee ou ajoutee, faites un cliche avant et comparez lors de la notification...

Le code n'est pas commente mais il est relativement simple a comprendre, je n'ai mis que le stricte necessaire...

J'ai mis un petit exemple d'utilisation dans le Zip...

Source / Exemple :


Imports System.Threading

Public Class NotifyClass

    Private Declare Function RegNotifyChangeKeyValue Lib "advapi32" (ByVal hKey As Integer, ByVal bWatchSubtree As Boolean, ByVal dwNotifyFilter As Integer, ByVal hEvent As Integer, ByVal fAsynchronous As Boolean) As Long
    Private Declare Function RegOpenKeyEx Lib "ADVAPI32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByVal ulOptions As Integer, ByVal samDesired As Integer, ByRef phkResult As Integer) As Integer
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Integer) As Integer

    Public Enum NotifycationType As Integer
        REG_NOTIFY_CHANGE_NAME = &H1
        REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
        REG_NOTIFY_CHANGE_LAST_SET = &H4
        REG_NOTIFY_CHANGE_SECURITY = &H8
        REG_NOTIFY_ALL = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
    End Enum

    Private Enum RegRacine As Integer
        HKEY_CLASSES_ROOT = &H80000000
        HKEY_CURRENT_USER = &H80000001
        HKEY_LOCAL_MACHINE = &H80000002
        HKEY_USERS = &H80000003
        HKEY_PERFORMANCE_DATA = &H80000004
        HKEY_CURRENT_CONFIG = &H80000005
        HKEY_DYN_DATA = &H80000006
    End Enum

    Private Const KEY_NOTIFY As Integer = &H10
    Private Const ERROR_SUCCES As Integer = 0&

    Private Rc As RegRacine
    Private sKey As String
    Private FullPathKey As String
    Private sStop As Boolean = False
    Private NotifyThread As Thread
    Private IsRunning As Boolean = False
    Private Hwnd As Integer
    Private Filter As NotifycationType = NotifycationType.REG_NOTIFY_ALL
    Private WatchSubKey As Boolean = False

    Public ReadOnly Property IsWatching() As Boolean

        Get
            Return IsRunning
        End Get

    End Property

    Public Property KeyFullPath() As String

        Get
            Return FullPathKey
        End Get

        Set(ByVal value As String)
            FullPathKey = value
            Dim pos As Integer
            Dim strRacine As String
            pos = InStr(FullPathKey, "\")
            If pos < 10 Then Exit Property
            sKey = Microsoft.VisualBasic.Right(FullPathKey, Len(FullPathKey) - pos)
            strRacine = Microsoft.VisualBasic.Left(FullPathKey, pos - 1)
            Rc = StrRacineToIntRacine(strRacine)
        End Set

    End Property

    Public Property WatchSubTree() As Boolean

        Get
            Return WatchSubKey
        End Get
        Set(ByVal value As Boolean)
            WatchSubKey = value
        End Set

    End Property

    Public Sub StartNotification()

        sStop = False
        NotifyThread = New Thread(AddressOf WaitChangeKey)
        NotifyThread.IsBackground = True
        NotifyThread.Start()

    End Sub

    Public Sub StopWatch()

        sStop = True
        RegCloseKey(Hwnd)

    End Sub

    Public Property NotifyFilter() As NotifycationType

        Get
            Return Filter
        End Get
        Set(ByVal value As NotifycationType)
            Filter = value
        End Set

    End Property

    Private Sub WaitChangeKey()

        Dim Result As Integer
        IsRunning = True

        Do
            Try
                Result = RegOpenKeyEx(Rc, sKey, 0, KEY_NOTIFY, Hwnd)
                If Result <> ERROR_SUCCES Then
                    MessageBox.Show("Error: open key:" & vbCrLf & FullPathKey, "Notification Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    IsRunning = False
                    Exit Sub
                End If
                RegNotifyChangeKeyValue(Hwnd, WatchSubKey, Filter, 0&, False)
                RegCloseKey(Hwnd)
                If sStop = True Then Exit Do
                ShowNotifycation()
            Catch ex As Exception
                IsRunning = False
                MessageBox.Show(ex.Message & vbCrLf & FullPathKey, "Notification Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End Try
        Loop

        IsRunning = False
        If sStop = False Then MessageBox.Show("Notification has been aborded", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Function StrRacineToIntRacine(ByVal StrRacine As String) As RegRacine

        Select Case StrRacine
            Case "HKEY_CLASSES_ROOT"
                Return RegRacine.HKEY_CLASSES_ROOT
            Case "HKEY_CURRENT_USER"
                Return RegRacine.HKEY_CURRENT_USER
            Case "HKEY_LOCAL_MACHINE"
                Return RegRacine.HKEY_LOCAL_MACHINE
            Case "HKEY_USERS"
                Return RegRacine.HKEY_USERS
            Case "HKEY_PERFORMANCE_DATA"
                Return RegRacine.HKEY_PERFORMANCE_DATA
            Case "HKEY_CURRENT_CONFIG"
                Return RegRacine.HKEY_CURRENT_CONFIG
            Case "HKEY_DYN_DATA"
                Return RegRacine.HKEY_DYN_DATA
        End Select

    End Function

    Private Sub ShowNotifycation()

        Select Case Filter
            Case NotifycationType.REG_NOTIFY_ALL
                MessageBox.Show(FullPathKey & vbCrLf & "has been modified", "REG_NOTIFY_ALL", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Case NotifycationType.REG_NOTIFY_CHANGE_ATTRIBUTES
                MessageBox.Show(FullPathKey & vbCrLf & "has been modified", "REG_NOTIFY_CHANGE_ATTRIBUTES", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Case NotifycationType.REG_NOTIFY_CHANGE_LAST_SET
                MessageBox.Show(FullPathKey & vbCrLf & "has been modified", "REG_NOTIFY_CHANGE_LAST_SET", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Case NotifycationType.REG_NOTIFY_CHANGE_NAME
                MessageBox.Show(FullPathKey & vbCrLf & "has been modified", "REG_NOTIFY_CHANGE_NAME", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Case NotifycationType.REG_NOTIFY_CHANGE_SECURITY
                MessageBox.Show(FullPathKey & vbCrLf & "has been modified", "REG_NOTIFY_CHANGE_SECURITY", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Select

    End Sub

End Class

Codes Sources

A voir également

Ajouter un commentaire Commentaires
Messages postés
551
Date d'inscription
samedi 8 janvier 2005
Statut
Membre
Dernière intervention
12 avril 2008
2
Salut : très bien cette source ! très utile ...

est-ce possible d'avoir la totalité du registre sous surveillance ?
Messages postés
2375
Date d'inscription
jeudi 12 juillet 2001
Statut
Modérateur
Dernière intervention
15 décembre 2018
22
Tiens un ch'tit concurrent de Regmon de Sysinternals :)

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.