From 745185fd8c8994984e987bf0aa3829f61722d300 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 10 Sep 2018 22:39:45 -0700 Subject: [PATCH] Move registry interop to shared CoreLib partition (#19886) --- .../Windows/Advapi32/Interop.RegCreateKeyEx.cs | 27 +++++ .../Windows/Advapi32/Interop.RegDeleteKeyEx.cs | 16 +++ .../Windows/Advapi32/Interop.RegDeleteValue.cs | 16 +++ .../Windows/Advapi32/Interop.RegEnumKeyEx.cs | 25 +++++ .../Windows/Advapi32/Interop.RegEnumValue.cs | 24 +++++ .../Windows/Advapi32/Interop.RegOpenKeyEx.cs | 30 ++++++ .../Windows/Advapi32/Interop.RegQueryInfoKey.cs | 29 ++++++ .../Windows/Advapi32/Interop.RegQueryValueEx.cs | 59 +++++++++++ .../Windows/Advapi32/Interop.RegSetValueEx.cs | 59 +++++++++++ .../Windows/Kernel32/Interop.RegistryView.cs | 15 --- .../shared/Microsoft/Win32/RegistryView.cs | 4 +- .../shared/System.Private.CoreLib.Shared.projitems | 12 ++- .../src/Microsoft/Win32/RegistryKey.cs | 62 +++++------ .../src/Microsoft/Win32/Win32Native.cs | 116 --------------------- 14 files changed, 328 insertions(+), 166 deletions(-) create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegCreateKeyEx.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteKeyEx.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteValue.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumKeyEx.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumValue.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegOpenKeyEx.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryInfoKey.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryValueEx.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegSetValueEx.cs delete mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.RegistryView.cs diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegCreateKeyEx.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegCreateKeyEx.cs new file mode 100644 index 0000000..e521906 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegCreateKeyEx.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Advapi32 + { + // Note: RegCreateKeyEx won't set the last error on failure - it returns + // an error code if it fails. + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegCreateKeyExW")] + internal static extern int RegCreateKeyEx( + SafeRegistryHandle hKey, + string lpSubKey, + int Reserved, + string lpClass, + int dwOptions, + int samDesired, + ref Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs, + out SafeRegistryHandle hkResult, + out int lpdwDisposition); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteKeyEx.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteKeyEx.cs new file mode 100644 index 0000000..4429515 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteKeyEx.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegDeleteKeyExW")] + internal static extern int RegDeleteKeyEx(SafeRegistryHandle hKey, string lpSubKey, int samDesired, int Reserved); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteValue.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteValue.cs new file mode 100644 index 0000000..b8044bc --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegDeleteValue.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegDeleteValueW")] + internal static extern int RegDeleteValue(SafeRegistryHandle hKey, string lpValueName); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumKeyEx.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumKeyEx.cs new file mode 100644 index 0000000..bedf282 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumKeyEx.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; +using System.Text; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegEnumKeyExW")] + internal static extern unsafe int RegEnumKeyEx( + SafeRegistryHandle hKey, + int dwIndex, + char[] lpName, + ref int lpcbName, + int[] lpReserved, + [Out]StringBuilder lpClass, + int[] lpcbClass, + long[] lpftLastWriteTime); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumValue.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumValue.cs new file mode 100644 index 0000000..e02ba98 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegEnumValue.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegEnumValueW")] + internal static extern unsafe int RegEnumValue( + SafeRegistryHandle hKey, + int dwIndex, + char[] lpValueName, + ref int lpcbValueName, + IntPtr lpReserved_MustBeZero, + int[] lpType, + byte[] lpData, + int[] lpcbData); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegOpenKeyEx.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegOpenKeyEx.cs new file mode 100644 index 0000000..86b6bca --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegOpenKeyEx.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegOpenKeyExW")] + internal static extern int RegOpenKeyEx( + SafeRegistryHandle hKey, + string lpSubKey, + int ulOptions, + int samDesired, + out SafeRegistryHandle hkResult); + + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegOpenKeyExW")] + internal static extern int RegOpenKeyEx( + IntPtr hKey, + string lpSubKey, + int ulOptions, + int samDesired, + out SafeRegistryHandle hkResult); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryInfoKey.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryInfoKey.cs new file mode 100644 index 0000000..2df2092 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryInfoKey.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; +using System.Text; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryInfoKeyW")] + internal static extern int RegQueryInfoKey( + SafeRegistryHandle hKey, + [Out]StringBuilder lpClass, + int[] lpcbClass, + IntPtr lpReserved_MustBeZero, + ref int lpcSubKeys, + int[] lpcbMaxSubKeyLen, + int[] lpcbMaxClassLen, + ref int lpcValues, + int[] lpcbMaxValueNameLen, + int[] lpcbMaxValueLen, + int[] lpcbSecurityDescriptor, + int[] lpftLastWriteTime); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryValueEx.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryValueEx.cs new file mode 100644 index 0000000..c6a8798 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegQueryValueEx.cs @@ -0,0 +1,59 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; +using System.Text; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW")] + internal static extern int RegQueryValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int[] lpReserved, + ref int lpType, + [Out] byte[] lpData, + ref int lpcbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW")] + internal static extern int RegQueryValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int[] lpReserved, + ref int lpType, + ref int lpData, + ref int lpcbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW")] + internal static extern int RegQueryValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int[] lpReserved, + ref int lpType, + ref long lpData, + ref int lpcbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW")] + internal static extern int RegQueryValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int[] lpReserved, + ref int lpType, + [Out] char[] lpData, + ref int lpcbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegQueryValueExW")] + internal static extern int RegQueryValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int[] lpReserved, + ref int lpType, + [Out]StringBuilder lpData, + ref int lpcbData); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegSetValueEx.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegSetValueEx.cs new file mode 100644 index 0000000..d46f848 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Advapi32/Interop.RegSetValueEx.cs @@ -0,0 +1,59 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.Win32; +using Microsoft.Win32.SafeHandles; +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Advapi32 + { + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegSetValueExW")] + internal static extern int RegSetValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int Reserved, + RegistryValueKind dwType, + byte[] lpData, + int cbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegSetValueExW")] + internal static extern int RegSetValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int Reserved, + RegistryValueKind dwType, + char[] lpData, + int cbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegSetValueExW")] + internal static extern int RegSetValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int Reserved, + RegistryValueKind dwType, + ref int lpData, + int cbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegSetValueExW")] + internal static extern int RegSetValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int Reserved, + RegistryValueKind dwType, + ref long lpData, + int cbData); + + [DllImport(Libraries.Advapi32, CharSet = CharSet.Unicode, BestFitMapping = false, EntryPoint = "RegSetValueExW")] + internal static extern int RegSetValueEx( + SafeRegistryHandle hKey, + string lpValueName, + int Reserved, + RegistryValueKind dwType, + string lpData, + int cbData); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.RegistryView.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.RegistryView.cs deleted file mode 100644 index 2e67868..0000000 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.RegistryView.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -internal partial class Interop -{ - internal partial class Kernel32 - { - internal partial class RegistryView - { - internal const int KEY_WOW64_64KEY = 0x0100; - internal const int KEY_WOW64_32KEY = 0x0200; - } - } -} diff --git a/src/System.Private.CoreLib/shared/Microsoft/Win32/RegistryView.cs b/src/System.Private.CoreLib/shared/Microsoft/Win32/RegistryView.cs index 3cc4a78..0d6b303 100644 --- a/src/System.Private.CoreLib/shared/Microsoft/Win32/RegistryView.cs +++ b/src/System.Private.CoreLib/shared/Microsoft/Win32/RegistryView.cs @@ -14,7 +14,7 @@ namespace Microsoft.Win32 enum RegistryView { Default = 0, // 0x0000 operate on the default registry view - Registry64 = Interop.Kernel32.RegistryView.KEY_WOW64_64KEY, // 0x0100 operate on the 64-bit registry view - Registry32 = Interop.Kernel32.RegistryView.KEY_WOW64_32KEY, // 0x0200 operate on the 32-bit registry view + Registry64 = Interop.Advapi32.RegistryView.KEY_WOW64_64KEY, // 0x0100 operate on the 64-bit registry view + Registry32 = Interop.Advapi32.RegistryView.KEY_WOW64_32KEY, // 0x0200 operate on the 32-bit registry view }; } diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index b2db71d..eae7016 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -802,8 +802,17 @@ + + + + + + + + + + - @@ -813,7 +822,6 @@ - diff --git a/src/System.Private.CoreLib/src/Microsoft/Win32/RegistryKey.cs b/src/System.Private.CoreLib/src/Microsoft/Win32/RegistryKey.cs index 25aa39b..52eeaae 100644 --- a/src/System.Private.CoreLib/src/Microsoft/Win32/RegistryKey.cs +++ b/src/System.Private.CoreLib/src/Microsoft/Win32/RegistryKey.cs @@ -277,7 +277,7 @@ namespace Microsoft.Win32 public void DeleteValue(string name, bool throwOnMissingValue) { EnsureWriteable(); - int errorCode = Win32Native.RegDeleteValue(_hkey, name); + int errorCode = Interop.Advapi32.RegDeleteValue(_hkey, name); // // From windows 2003 server, if the name is too long we will get error code ERROR_FILENAME_EXCED_RANGE @@ -351,7 +351,7 @@ namespace Microsoft.Win32 name = FixupName(name); // Fixup multiple slashes to a single slash SafeRegistryHandle result = null; - int ret = Win32Native.RegOpenKeyEx(_hkey, + int ret = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, GetRegistryKeyAccess(writable) | (int)_regView, @@ -403,7 +403,7 @@ namespace Microsoft.Win32 int result; int nameLength = name.Length; - while ((result = Win32Native.RegEnumKeyEx( + while ((result = Interop.Advapi32.RegEnumKeyEx( _hkey, names.Count, name, @@ -457,7 +457,7 @@ namespace Microsoft.Win32 int result; int nameLength = name.Length; - while ((result = Win32Native.RegEnumValue( + while ((result = Interop.Advapi32.RegEnumValue( _hkey, names.Count, name, @@ -567,7 +567,7 @@ namespace Microsoft.Win32 int type = 0; int datasize = 0; - int ret = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, (byte[])null, ref datasize); + int ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, (byte[])null, ref datasize); if (ret != 0) { @@ -578,7 +578,7 @@ namespace Microsoft.Win32 int r; byte[] blob = new byte[size]; - while (Interop.Errors.ERROR_MORE_DATA == (r = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, blob, ref sizeInput))) + while (Interop.Errors.ERROR_MORE_DATA == (r = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, blob, ref sizeInput))) { if (size == int.MaxValue) { @@ -621,47 +621,47 @@ namespace Microsoft.Win32 switch (type) { - case Win32Native.REG_NONE: - case Win32Native.REG_DWORD_BIG_ENDIAN: - case Win32Native.REG_BINARY: + case Interop.Advapi32.RegistryValues.REG_NONE: + case Interop.Advapi32.RegistryValues.REG_DWORD_BIG_ENDIAN: + case Interop.Advapi32.RegistryValues.REG_BINARY: { byte[] blob = new byte[datasize]; - ret = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); + ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); data = blob; } break; - case Win32Native.REG_QWORD: + case Interop.Advapi32.RegistryValues.REG_QWORD: { // also REG_QWORD_LITTLE_ENDIAN if (datasize > 8) { // prevent an AV in the edge case that datasize is larger than sizeof(long) - goto case Win32Native.REG_BINARY; + goto case Interop.Advapi32.RegistryValues.REG_BINARY; } long blob = 0; Debug.Assert(datasize == 8, "datasize==8"); // Here, datasize must be 8 when calling this - ret = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, ref blob, ref datasize); + ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, ref blob, ref datasize); data = blob; } break; - case Win32Native.REG_DWORD: + case Interop.Advapi32.RegistryValues.REG_DWORD: { // also REG_DWORD_LITTLE_ENDIAN if (datasize > 4) { // prevent an AV in the edge case that datasize is larger than sizeof(int) - goto case Win32Native.REG_QWORD; + goto case Interop.Advapi32.RegistryValues.REG_QWORD; } int blob = 0; Debug.Assert(datasize == 4, "datasize==4"); // Here, datasize must be four when calling this - ret = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, ref blob, ref datasize); + ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, ref blob, ref datasize); data = blob; } break; - case Win32Native.REG_SZ: + case Interop.Advapi32.RegistryValues.REG_SZ: { if (datasize % 2 == 1) { @@ -677,7 +677,7 @@ namespace Microsoft.Win32 } char[] blob = new char[datasize / 2]; - ret = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); + ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); if (blob.Length > 0 && blob[blob.Length - 1] == (char)0) { data = new string(blob, 0, blob.Length - 1); @@ -691,7 +691,7 @@ namespace Microsoft.Win32 } break; - case Win32Native.REG_EXPAND_SZ: + case Interop.Advapi32.RegistryValues.REG_EXPAND_SZ: { if (datasize % 2 == 1) { @@ -707,7 +707,7 @@ namespace Microsoft.Win32 } char[] blob = new char[datasize / 2]; - ret = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); + ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); if (blob.Length > 0 && blob[blob.Length - 1] == (char)0) { @@ -724,7 +724,7 @@ namespace Microsoft.Win32 data = Environment.ExpandEnvironmentVariables((string)data); } break; - case Win32Native.REG_MULTI_SZ: + case Interop.Advapi32.RegistryValues.REG_MULTI_SZ: { if (datasize % 2 == 1) { @@ -740,7 +740,7 @@ namespace Microsoft.Win32 } char[] blob = new char[datasize / 2]; - ret = Win32Native.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); + ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, blob, ref datasize); // make sure the string is null terminated before processing the data if (blob.Length > 0 && blob[blob.Length - 1] != (char)0) @@ -801,7 +801,7 @@ namespace Microsoft.Win32 strings.CopyTo((string[])data, 0); } break; - case Win32Native.REG_LINK: + case Interop.Advapi32.RegistryValues.REG_LINK: default: break; } @@ -851,7 +851,7 @@ namespace Microsoft.Win32 case RegistryValueKind.String: { string data = value.ToString(); - ret = Win32Native.RegSetValueEx(_hkey, + ret = Interop.Advapi32.RegSetValueEx(_hkey, name, 0, valueKind, @@ -898,7 +898,7 @@ namespace Microsoft.Win32 *(char*)(currentPtr.ToPointer()) = '\0'; currentPtr = new IntPtr((long)currentPtr + 2); - ret = Win32Native.RegSetValueEx(_hkey, + ret = Interop.Advapi32.RegSetValueEx(_hkey, name, 0, RegistryValueKind.MultiString, @@ -911,10 +911,10 @@ namespace Microsoft.Win32 case RegistryValueKind.None: case RegistryValueKind.Binary: byte[] dataBytes = (byte[])value; - ret = Win32Native.RegSetValueEx(_hkey, + ret = Interop.Advapi32.RegSetValueEx(_hkey, name, 0, - (valueKind == RegistryValueKind.None ? Win32Native.REG_NONE : RegistryValueKind.Binary), + (valueKind == RegistryValueKind.None ? Interop.Advapi32.RegistryValues.REG_NONE : RegistryValueKind.Binary), dataBytes, dataBytes.Length); break; @@ -925,7 +925,7 @@ namespace Microsoft.Win32 // unboxed and cast at the same time. I.e. ((int)(object)(short) 5) will fail. int data = Convert.ToInt32(value, System.Globalization.CultureInfo.InvariantCulture); - ret = Win32Native.RegSetValueEx(_hkey, + ret = Interop.Advapi32.RegSetValueEx(_hkey, name, 0, RegistryValueKind.DWord, @@ -938,7 +938,7 @@ namespace Microsoft.Win32 { long data = Convert.ToInt64(value, System.Globalization.CultureInfo.InvariantCulture); - ret = Win32Native.RegSetValueEx(_hkey, + ret = Interop.Advapi32.RegSetValueEx(_hkey, name, 0, RegistryValueKind.QWord, @@ -1074,11 +1074,11 @@ namespace Microsoft.Win32 int winAccess; if (!isWritable) { - winAccess = Win32Native.KEY_READ; + winAccess = Interop.Advapi32.RegistryOperations.KEY_READ; } else { - winAccess = Win32Native.KEY_READ | Win32Native.KEY_WRITE; + winAccess = Interop.Advapi32.RegistryOperations.KEY_READ | Interop.Advapi32.RegistryOperations.KEY_WRITE; } return winAccess; diff --git a/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs b/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs index d1a92a3..45ce42a 100644 --- a/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs +++ b/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs @@ -112,50 +112,6 @@ namespace Microsoft.Win32 internal static class Win32Native { - internal const int KEY_QUERY_VALUE = 0x0001; - internal const int KEY_SET_VALUE = 0x0002; - internal const int KEY_CREATE_SUB_KEY = 0x0004; - internal const int KEY_ENUMERATE_SUB_KEYS = 0x0008; - internal const int KEY_NOTIFY = 0x0010; - internal const int KEY_CREATE_LINK = 0x0020; - internal const int KEY_READ = ((STANDARD_RIGHTS_READ | - KEY_QUERY_VALUE | - KEY_ENUMERATE_SUB_KEYS | - KEY_NOTIFY) - & - (~SYNCHRONIZE)); - - internal const int KEY_WRITE = ((STANDARD_RIGHTS_WRITE | - KEY_SET_VALUE | - KEY_CREATE_SUB_KEY) - & - (~SYNCHRONIZE)); - internal const int REG_OPTION_NON_VOLATILE = 0x0000; // (default) keys are persisted beyond reboot/unload - internal const int REG_OPTION_VOLATILE = 0x0001; // All keys created by the function are volatile - internal const int REG_OPTION_CREATE_LINK = 0x0002; // They key is a symbolic link - internal const int REG_OPTION_BACKUP_RESTORE = 0x0004; // Use SE_BACKUP_NAME process special privileges - internal const int REG_NONE = 0; // No value type - internal const int REG_SZ = 1; // Unicode nul terminated string - internal const int REG_EXPAND_SZ = 2; // Unicode nul terminated string - // (with environment variable references) - internal const int REG_BINARY = 3; // Free form binary - internal const int REG_DWORD = 4; // 32-bit number - internal const int REG_DWORD_LITTLE_ENDIAN = 4; // 32-bit number (same as REG_DWORD) - internal const int REG_DWORD_BIG_ENDIAN = 5; // 32-bit number - internal const int REG_LINK = 6; // Symbolic Link (unicode) - internal const int REG_MULTI_SZ = 7; // Multiple Unicode strings - internal const int REG_RESOURCE_LIST = 8; // Resource list in the resource map - internal const int REG_FULL_RESOURCE_DESCRIPTOR = 9; // Resource list in the hardware description - internal const int REG_RESOURCE_REQUIREMENTS_LIST = 10; - internal const int REG_QWORD = 11; // 64-bit number - - // Win32 ACL-related constants: - internal const int READ_CONTROL = 0x00020000; - internal const int SYNCHRONIZE = 0x00100000; - - internal const int STANDARD_RIGHTS_READ = READ_CONTROL; - internal const int STANDARD_RIGHTS_WRITE = READ_CONTROL; - internal const int LMEM_FIXED = 0x0000; internal const int LMEM_ZEROINIT = 0x0040; internal const int LPTR = (LMEM_FIXED | LMEM_ZEROINIT); @@ -184,15 +140,6 @@ namespace Microsoft.Win32 } [StructLayout(LayoutKind.Sequential)] - internal class SECURITY_ATTRIBUTES - { - internal int nLength = 0; - // don't remove null, or this field will disappear in bcl.small - internal unsafe byte* pSecurityDescriptor = null; - internal int bInheritHandle = 0; - } - - [StructLayout(LayoutKind.Sequential)] internal struct MEMORYSTATUSEX { // The length field must be set to the size of this data structure. @@ -220,11 +167,6 @@ namespace Microsoft.Win32 } internal const string ADVAPI32 = "advapi32.dll"; - internal const string SHELL32 = "shell32.dll"; - internal const string SHIM = "mscoree.dll"; - internal const string CRYPT32 = "crypt32.dll"; - internal const string SECUR32 = "secur32.dll"; - internal const string MSCORWKS = "coreclr.dll"; [DllImport(Interop.Libraries.Kernel32, EntryPoint = "LocalAlloc")] internal static extern IntPtr LocalAlloc_NoSafeHandle(int uFlags, UIntPtr sizetdwBytes); @@ -332,64 +274,6 @@ namespace Microsoft.Win32 [DllImport(Interop.Libraries.Ole32)] internal static extern IntPtr CoTaskMemRealloc(IntPtr pv, UIntPtr cb); -#if FEATURE_WIN32_REGISTRY - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegDeleteValue(SafeRegistryHandle hKey, string lpValueName); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern unsafe int RegEnumKeyEx(SafeRegistryHandle hKey, int dwIndex, - char[] lpName, ref int lpcbName, int[] lpReserved, - [Out]StringBuilder lpClass, int[] lpcbClass, - long[] lpftLastWriteTime); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern unsafe int RegEnumValue(SafeRegistryHandle hKey, int dwIndex, - char[] lpValueName, ref int lpcbValueName, - IntPtr lpReserved_MustBeZero, int[] lpType, byte[] lpData, - int[] lpcbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegOpenKeyEx(SafeRegistryHandle hKey, string lpSubKey, - int ulOptions, int samDesired, out SafeRegistryHandle hkResult); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, - int[] lpReserved, ref int lpType, [Out] byte[] lpData, - ref int lpcbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, - int[] lpReserved, ref int lpType, ref int lpData, - ref int lpcbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, - int[] lpReserved, ref int lpType, ref long lpData, - ref int lpcbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, - int[] lpReserved, ref int lpType, [Out] char[] lpData, - ref int lpcbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, - int Reserved, RegistryValueKind dwType, byte[] lpData, int cbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, - int Reserved, RegistryValueKind dwType, ref int lpData, int cbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, - int Reserved, RegistryValueKind dwType, ref long lpData, int cbData); - - [DllImport(ADVAPI32, CharSet = CharSet.Auto, BestFitMapping = false)] - internal static extern int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, - int Reserved, RegistryValueKind dwType, string lpData, int cbData); -#endif // FEATURE_WIN32_REGISTRY - [DllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false)] internal static extern int ExpandEnvironmentStrings(string lpSrc, [Out]StringBuilder lpDst, int nSize); -- 2.7.4