繁体   English   中英

如何在C#中使用pinvoke regcreatekeyEx监视注册表项?

[英]How do I monitor registry key using pinvoke regcreatekeyEx in C#?

我正在用C#开发注册表监视应用程序以创建注册表项。 我尝试使用p / invoke-如果不存在则创建密钥,但是如果确实存在则返回错误值5-访问被拒绝错误。

 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(); 
} 
}

我在这里想念什么?

.net内置注册表类中是否有可用的类似API(RegNotifyChangeKeyValue())?

尝试这个。 这似乎是创建注册表监视器的一种非常流行的解决方案: RegistryMonitor-RegNotifyChangeKeyValue的.NET包装器类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM