Implement SafeRegistryHandle.ReleaseHandle and update call sites.
authorCarlos Alberto Cortez <calberto.cortez@gmail.com>
Sat, 7 Aug 2010 01:44:30 +0000 (03:44 +0200)
committerCarlos Alberto Cortez <calberto.cortez@gmail.com>
Sat, 7 Aug 2010 08:19:49 +0000 (10:19 +0200)
* Microsoft.Win32.SafeHandles/SafeRegistryHandle.cs: Implement
ReleaseHandle by pinvoking RegCloseKey if we are on windows, and
nothing otherwise.

* Microsoft.Win32/Win32RegistryApi.cs:
* Microsoft.Win32/RegistryKey.cs: Don't call SafeHandle.Close
from our public api, and do it instead un the windows layer, as
we only support it there for now.

mcs/class/corlib/Microsoft.Win32.SafeHandles/SafeRegistryHandle.cs
mcs/class/corlib/Microsoft.Win32/RegistryKey.cs
mcs/class/corlib/Microsoft.Win32/Win32RegistryApi.cs

index 5c83c25e34e74dcf51f90368d095dab53cc0b81e..e340b9e885796f463dc4e698b3462d531fc327bf 100644 (file)
@@ -28,6 +28,7 @@
 
 #if NET_4_0
 using System;
+using System.IO;
 using System.Runtime.InteropServices;
 using System.Runtime.ConstrainedExecution;
 
@@ -41,8 +42,15 @@ namespace Microsoft.Win32.SafeHandles {
 
                protected override bool ReleaseHandle ()
                {
-                       throw new NotImplementedException ();
+                       // Need to release an unmanaged pointer *only* in Windows.
+                       if (Path.DirectorySeparatorChar == '\\')
+                               return RegCloseKey (handle) == 0;
+
+                       return true;
                }
+
+               [DllImport ("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint="RegCloseKey")]
+               static extern int RegCloseKey (IntPtr keyHandle);
        }
 }
 #endif
index d9a5338a62941d604ea033ae238de00fc875aa50..1c202aa6717aa4464a23dcf97444fa1297b73b13 100644 (file)
@@ -164,10 +164,7 @@ namespace Microsoft.Win32
                        RegistryApi.Close (this);
                        handle = null;
 #if NET_4_0
-                       if (safe_handle != null) {
-                               safe_handle.Close ();
-                               safe_handle = null;
-                       }
+                       safe_handle = null;
 #endif
                }
                
index 472f4a73c5b9a4d076e0f205851f92d90d4b372c..4ad202c5194885962dd71565a6dfb4edd4c06e61 100644 (file)
@@ -424,6 +424,14 @@ namespace Microsoft.Win32
                {
                        if (!IsHandleValid (rkey))
                                return;
+#if NET_4_0
+                       SafeRegistryHandle safe_handle = rkey.Handle;
+                       if (safe_handle != null) {
+                               // closes the unmanaged pointer for us.
+                               safe_handle.Close ();
+                               return;
+                       }
+#endif
                        IntPtr handle = GetHandle (rkey);
                        RegCloseKey (handle);
                }