.NETGURU
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...
 
    
Dot Net Guruias 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.

Dot Net Guruias

--
Dot Net Guruias 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

"Dot Net Guruias Sjögren" wrote:

[Original message clipped]

Reply to this message...
 
    
Robert Jordan
Hi Dot Net Guruias,

[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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification