Search:
Namespaces
Discussions
.NET v1.1
Feedback
RegNotifyChangeKeyValue
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework.interop
.
Post a new message to this list...
TonyC
I have written a C# wapper around RegNotifyChangeKeyValue to handle Registry
change notifications. This works fine, except in Release builds, where I
seem to get spurious notifications. I've probably done womething stupid -
any ideas? The code is based on an MSDN article (If I remember correctly)
Many thanks
Tony
The declarations:
public enum NotifyFilterFlags
{
REG_NOTIFY_CHANGE_NAME = 1,
REG_NOTIFY_CHANGE_ATTRIBUTES = 2,
REG_NOTIFY_CHANGE_LAST_SET = 4,
REG_NOTIFY_CHANGE_SECURITY = 8
}
public const int ERROR_KEY_DELETED = 1018;
[DllImport("advapi32.dll", EntryPoint="RegNotifyChangeKeyValue",
CallingConvention
=
CallingConvention
.Cdecl)]
static public extern int RegNotifyChangeKeyValue(
IntPtr
key,
[MarshalAs(
UnmanagedType
.Bool)] bool watchsubtree,
NotifyFilterFlags flags,
IntPtr notifyEvent,
[MarshalAs(
UnmanagedType
.Bool)] bool asynch );
The calling code:
// handle to reg key
IntPtr KeyHandle;
// establish key
RegistryKey KeyToMonitor = this.m_RootKey.OpenSubKey( this.m_SubKey,
false );
// get Win32 stuff
Type regKeyType = typeof( RegistryKey );
FieldInfo field;
field = regKeyType.GetField( "hkey",
BindingFlags
.Instance |
BindingFlags
.NonPublic );
// and the actual win32 handle
KeyHandle = (IntPtr)field.GetValue(KeyToMonitor);
int err;
// call the reg function asynchronously
err = NativeMethods.RegNotifyChangeKeyValue(KeyHandle,
true,
REG_NOTIFY_CHANGE_ATTRIBUTES |
REG_NOTIFY_CHANGE_LAST_SET |
REG_NOTIFY_CHANGE_NAME |
REG_NOTIFY_CHANGE_SECURITY,
notifyEvent.Handle,
true );
if ( err == 0 )
{
bool fired = false;
while ( !fired && !m_bStop )
{
// wait max 1 sec
if ( notifyEvent.WaitOne( 1000, false ) )
{
// fired
KeyChanged( this, new
EventArgs
() );
fired = true;
}
}
--
Systems Development Consultant
Reply to this message...
Mattias Sjögren
Tony,
[Original message clipped]
The calling convention is wrong, you should use Stdcall (the default).
The MarshalAs attributes are redundant but not incorrect.
[Original message clipped]
This is the most broken part of your code. Reflecting on private
members is evil, don't do it.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
|
http://www.dotnetinterop.com
Please reply only to the newsgroup.
Reply to this message...
TonyC
Thanks for this. I changed the calling convention (silly mistake - too much
copy and paste!), but no change.
I re-packed the class slightly, and all the problems vanished (no core code
changes). So, I remain mystified, but now happy!.
Why can't you user Reflection on private members? My class still does this
(and it's documented more than once elsewhere). How else would you solve
that problem?
Regards
Tony
"Mattias Sjögren" wrote:
[Original message clipped]
Reply to this message...
Robert Jordan
Hi Mattias,
[Original message clipped]
Why? Even when the assembly is requesting FullTrust?
How will Formatters work in the future w/out this feature?
bye
Rob
Reply to this message...
System.EventArgs
System.IntPtr
System.Reflection.BindingFlags
System.Reflection.FieldInfo
System.Runtime.InteropServices.CallingConvention
System.Runtime.InteropServices.UnmanagedType
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com