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?