यह पहली बार है कि मैं SafeHandle
का उपयोग करूँगा।सुरक्षित हैंडल क्यों है। खतरनाक गेटहैंडल() "खतरनाक"?
मुझे इस पी/Invoke विधि को कॉल करने की आवश्यकता है जिसे UIntPtr की आवश्यकता है।
[DllImport("advapi32.dll", CharSet = CharSet.Auto)] public static extern int RegOpenKeyEx( UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult);
यह UIntPtr नेट के RegistryKey वर्ग से प्राप्त की जाएगी। मैं विधि का उपयोग किया जाएगा ऊपर एक IntPtr करने RegistryKey वर्ग कन्वर्ट करने के लिए तो मैं ऊपर पी उपयोग कर सकते हैं/आह्वान:
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;
}
}
सवाल:
- वहाँ का उपयोग किए बिना इस लिखने के लिए एक बेहतर तरीका है "असुरक्षित" हैंडल?
- असुरक्षित हैंडल खतरनाक क्यों हैं?
मैं यह उल्लेख करना भूल गया कि मेरे कोड का उद्देश्य नेटफैक्स 4 के 64-बिट रजिस्ट्री समर्थन की नकल करना है। हम केवल नेटफैक्स 3.5 का उपयोग कर रहे हैं, इसलिए कोई सुरक्षित रजिस्ट्री हैडल उपलब्ध नहीं है। – Ian
बस इसे सुरक्षित करेंडलजेरोरोमिनसऑनइन्स इनवालिड, सुरक्षित रजिस्ट्री हैडल का आधार वर्ग। या यदि आप नाम टाइप करना नफरत करते हैं (जो नहीं) तो केवल सादा सेफहैंडल। –