Я впервые буду использовать SafeHandle
.
Мне нужно вызвать этот метод P / Invoke, которому требуется UIntPtr.
[DllImport ("advapi32.dll", CharSet = CharSet.Auto)] общедоступный статический extern int RegOpenKeyEx ( UIntPtr hKey, строка subKey, int ulOptions, int samDesired, out UIntPtr hkResult);
Этот UIntPtr будет производным от класса .NET RegistryKey. Я буду использовать описанный выше метод для преобразования класса RegistryKey в IntPtr, чтобы я мог использовать приведенный выше P / Invoke:
private static IntPtr GetRegistryKeyHandle(RegistryKey rKey)
{
//Get the type of the RegistryKey
Type registryKeyType = typeof(RegistryKey);
//Get the FieldInfo of the 'hkey' member of RegistryKey
System.Reflection.FieldInfo fieldInfo =
registryKeyType.GetField("hkey", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
//Get the handle held by hkey
if (fieldInfo != null)
{
SafeHandle handle = (SafeHandle)fieldInfo.GetValue(rKey);
//Get the unsafe handle
IntPtr dangerousHandle = handle.DangerousGetHandle();
return dangerousHandle;
}
}
Вопросы: