0

I'm developing an registry monitoring application in C# to create a registry key. I have tried using p/invoke - it creates the key if it does not exist, but if it does exist then it returns error value 5 - access denied error.

 enum RegistryDispositionValue : uint
        {
            REG_CREATED_NEW_KEY = 0x00000001,
            REG_OPENED_EXISTING_KEY = 0x00000002
        }

     [DllImport("advapi32.dll", SetLastError = true)]
            private static extern int RegCreateKeyEx(
                        IntPtr hKey,
                        string lpSubKey,
                        uint reserved,
                        string lpClass,
                        uint dwOptions,
                        int samDesired,
                        IntPtr lpSecurityAttributes,
                        out IntPtr phkResult,
                        out RegistryDispositionValue lpdwDisposition);

     int iResult;
                IntPtr ipKey = IntPtr.Zero;
                string _registrySubName="SOFTWARE\XYZ\subkey";
                RegistryDispositionValue iDisposition;

                int result = RegCreateKeyEx(HKEY_LOCAL_MACHINE, _registrySubName, 0, null, REG_OPTION_VOLATILE , 0, IntPtr.Zero, out registryKey, out iDisposition);

AutoResetEvent _eventNotify = new AutoResetEvent(false); 
WaitHandle[] waitHandles = new WaitHandle[] {_eventNotify, _eventTerminate}; 
while (!_eventTerminate.WaitOne(0, true)) 
{ 
result = RegNotifyChangeKeyValue(registryKey, true, _regFilter, _eventNotify.Handle, true); 
if (result != 0) 
   throw new Win32Exception(result); 
if (WaitHandle.WaitAny(waitHandles) == 0) 
{ 
OnRegChanged(); 
} 
}

What am I missing here?

is there similar API (RegNotifyChangeKeyValue()) available in .net inbuilt registry class?

user3106005
  • 179
  • 3
  • 20
  • 4
    What's wrong with .NET's inbuilt registry handling? (Eg. [`Microsoft.Win32.RegistryKey`](http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey%28v=vs.110%29.aspx).) – Richard Oct 29 '14 at 13:19
  • 3
    You don't need to use a p/invoke layer to do this. You can use the [Registry](http://msdn.microsoft.com/en-us/library/microsoft.win32.registry(v=vs.110).aspx) class for this. – pstrjds Oct 29 '14 at 13:19
  • 2
    You cannot create keys in HKLM unless your program runs with [admin rights](http://stackoverflow.com/questions/2818179/how-to-force-my-net-app-to-run-as-administrator-on-windows-7) or you change the access rights of the registry key. Don't use HKLM, use HKCU. – Hans Passant Oct 29 '14 at 13:47
  • It's not even as if you are trying to use an alternate view, something that needs p/invoke for pre .net 4.0. FWIW, `SetLastError = true` is wrong for the registry APIs. Error codes are returned directly. – David Heffernan Oct 29 '14 at 14:02
  • There is a special function to [open](http://www.pinvoke.net/default.aspx/advapi32.regopenkeyex) key in winapi. But, as others already mentioned, in `C#` you can simply use `Registry`. – Sinatr Oct 29 '14 at 14:09
  • I'm writing an application to monitor specific registry key. – user3106005 Oct 30 '14 at 04:15
  • I'm writing an application where one application will create and then monitor created registry key and another application will modify the key value based on some event. for monitoring registry key, I'm using RegNotifyChangeKeyValue() WINAPI which require handle of registry key. first I created registry key using RegCreateEx() WINAPI and then pass the handled to RegNotifyChangeKeyValue() WINAPI I'm doing by below way – user3106005 Oct 30 '14 at 04:21
  • AutoResetEvent _eventNotify = new AutoResetEvent(false); WaitHandle[] waitHandles = new WaitHandle[] {_eventNotify, _eventTerminate}; while (!_eventTerminate.WaitOne(0, true)) { result = RegNotifyChangeKeyValue(registryKey, true, _regFilter, _eventNotify.Handle, true); if (result != 0) throw new Win32Exception(result); if (WaitHandle.WaitAny(waitHandles) == 0) { OnRegChanged(); } } – user3106005 Oct 30 '14 at 04:21
  • is there similar API(RegNotifyChangeKeyValue()) available in .net inbuilt registry? – user3106005 Oct 30 '14 at 05:53

1 Answers1

0

Try this. It seems to be a pretty popular solution for creating a Registry Monitor: RegistryMonitor - a .NET wrapper class for RegNotifyChangeKeyValue

Xcalibur37
  • 2,305
  • 1
  • 17
  • 20