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.
RegNotifyChangeKeyValue (advapi32)
.
C# Signature:
[DllImport("advapi32.dll", SetLastError=true)]
static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool watchSubtree,
int dwNotifyFilter, IntPtr hEvent, bool fAsynchronous);
VB Signature:
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.
[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
}
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32;
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;
}
#region Enumerations
[Flags]
public enum REG_NOTIFY_CHANGE : uint
{
NAME = 0x1,
ATTRIBUTES = 0x2,
LAST_SET = 0x4,
SECURITY = 0x8
// The "Close()" will trigger RegNotifyChangeKeyValue if it is still listening
if (this._monitorKey != null)
{
this._monitorKey.Close();
this._monitorKey = null;
}
}
}
#endregion
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);
}
this.Changed(this, e);
break;
if (e.Stop) break;
}
}
}
}
}
catch (Exception ex)
{
if (this.Error != null)
{
RegistryChangeEventArgs e = new RegistryChangeEventArgs(this);
e.Exception = ex;
this.Error(this, e);
}
}
finally
{
this.Stop();
}
}
#endregion
else
{
if (this.Error != null)
{
Win32Exception ex = new Win32Exception();
#region Events
public event RegistryChangeHandler Changed;
public event RegistryChangeHandler Error;
#endregion
// 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) }
);
#region Properties
public bool Monitoring
{
get
{
if (this._monitorThread != null)
return this._monitorThread.IsAlive;
RegistryChangeEventArgs e = new RegistryChangeEventArgs(this);
e.Exception = ex;
this.Error(this, e);
}
#region Properties
public RegistryChangeMonitor Monitor
public bool Monitoring
{
get { return this._monitor; }
}
get
{
if (this._monitorThread != null)
return this._monitorThread.IsAlive;
public Exception Exception
{
get { return this._exception; }
set { this._exception = value; }
return false;
}
}
public bool Stop
{
get { return this._stop; }
set { this._stop = value; }
}
#endregion
}
}
#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
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).