Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than advapi32, prefix the name with the module name and a period.
[Flags]
public enum REG_NOTIFY_CHANGE : uint
{
/// <summary>
/// Notify the caller if a subkey is added or deleted
/// </summary>
NAME = 0x1,
/// <summary>
/// Notify the caller of changes to the attributes of the key,
/// such as the security descriptor information
/// </summary>
ATTRIBUTES = 0x2,
/// <summary>
/// Notify the caller of changes to a value of the key. This can
/// include adding or deleting a value, or changing an existing value
/// </summary>
LAST_SET = 0x4,
/// <summary>
/// Notify the caller of changes to the security descriptor of the key
/// </summary>
SECURITY = 0x8
}
Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" (hKey As IntPtr, _
watchSubtree As Boolean, dwNotifyFilter As Integer, hEvent As IntPtr,
fAsynchronous As Boolean) As Integer
Notes:
Starting with the .NET Framework 2.0, the IntPtr parameters could be defined as SafeHandle instead.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32;
#region Methods
private void Dispose(bool disposing)
{
if (disposing)
GC.SuppressFinalize(this);
this.Stop();
}
public void Dispose()
{
this.Dispose(true);
}
public void Start()
{
lock (this)
{
if (this._monitorThread == null)
{
ThreadStart ts = new ThreadStart(this.MonitorThread);
this._monitorThread = new Thread(ts);
this._monitorThread.IsBackground = true;
}
if (!this._monitorThread.IsAlive)
{
this._monitorThread.Start();
}
}
}
public void Stop()
{
lock (this)
{
this.Changed = null;
this.Error = null;
if (this._monitorThread != null)
{
this._monitorThread = null;
}
// The "Close()" will trigger RegNotifyChangeKeyValue if it is still listening
if (this._monitorKey != null)
{
this._monitorKey.Close();
this._monitorKey = null;
}
}
}
private void MonitorThread()
{
try
{
IntPtr ptr = IntPtr.Zero;
lock (this)
{
if (this._registryPath.StartsWith("HKEY_CLASSES_ROOT"))
this._monitorKey = Registry.ClassesRoot.OpenSubKey(this._registryPath.Substring(18));
else if (this._registryPath.StartsWith("HKCR"))
this._monitorKey = Registry.ClassesRoot.OpenSubKey(this._registryPath.Substring(5));
else if (this._registryPath.StartsWith("HKEY_CURRENT_USER"))
this._monitorKey = Registry.CurrentUser.OpenSubKey(this._registryPath.Substring(18));
else if (this._registryPath.StartsWith("HKCU"))
this._monitorKey = Registry.CurrentUser.OpenSubKey(this._registryPath.Substring(5));
else if (this._registryPath.StartsWith("HKEY_LOCAL_MACHINE"))
this._monitorKey = Registry.LocalMachine.OpenSubKey(this._registryPath.Substring(19));
else if (this._registryPath.StartsWith("HKLM"))
this._monitorKey = Registry.LocalMachine.OpenSubKey(this._registryPath.Substring(5));
else if (this._registryPath.StartsWith("HKEY_USERS"))
this._monitorKey = Registry.Users.OpenSubKey(this._registryPath.Substring(11));
else if (this._registryPath.StartsWith("HKU"))
this._monitorKey = Registry.Users.OpenSubKey(this._registryPath.Substring(4));
else if (this._registryPath.StartsWith("HKEY_CURRENT_CONFIG"))
this._monitorKey = Registry.CurrentConfig.OpenSubKey(this._registryPath.Substring(20));
else if (this._registryPath.StartsWith("HKCC"))
this._monitorKey = Registry.CurrentConfig.OpenSubKey(this._registryPath.Substring(5));
if (ptr != IntPtr.Zero)
{
while (true)
{
// If this._monitorThread is null that probably means Dispose is being called. Don't monitor anymore.
if ((this._monitorThread == null) || (this._monitorKey == null))
break;
// RegNotifyChangeKeyValue blocks until a change occurs.
int result = RegNotifyChangeKeyValue(ptr, true, this._filter, IntPtr.Zero, false);
if ((this._monitorThread == null) || (this._monitorKey == null))
break;
if (result == 0)
{
if (this.Changed != null)
{
RegistryChangeEventArgs e = new RegistryChangeEventArgs(this);
this.Changed(this, e);
if (e.Stop) break;
}
}
else
{
if (this.Error != null)
{
Win32Exception ex = new Win32Exception();
// Unless the exception is thrown, nobody is nice enough to set a good stacktrace for us. Set it ourselves.
typeof(Exception).InvokeMember(
"_stackTrace",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField,
null,
ex,
new object[] { new StackTrace(true) }
);
RegistryChangeEventArgs e = new RegistryChangeEventArgs(this);
e.Exception = ex;
this.Error(this, e);
}
#region Properties
public RegistryChangeMonitor Monitor
{
get { return this._monitor; }
}
public Exception Exception
{
get { return this._exception; }
set { this._exception = value; }
}
public bool Stop
{
get { return this._stop; }
set { this._stop = value; }
}
#endregion
}
}
Alternative Managed API:
Do you know one? Please contribute it!
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
TODO - a short description
5/10/2012 11:22:02 AM - anonymous
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).