diff --git a/PInvoke/Shared/Registry.cs b/PInvoke/Shared/Registry.cs index 733ff8b2..7a9a6a3f 100644 --- a/PInvoke/Shared/Registry.cs +++ b/PInvoke/Shared/Registry.cs @@ -1,4 +1,5 @@ using System; +using Vanara.InteropServices; namespace Vanara.PInvoke { @@ -14,9 +15,11 @@ namespace Vanara.PInvoke REG_NONE = 0, /// Binary data in any form. + [CorrespondingType(typeof(byte[]))] REG_BINARY = 3, /// A 32-bit number. + [CorrespondingType(typeof(uint))] REG_DWORD = 4, /// @@ -24,6 +27,7 @@ namespace Vanara.PInvoke /// Windows is designed to run on little-endian computer architectures. /// Therefore, this value is defined as REG_DWORD in the Windows header files. /// + [CorrespondingType(typeof(uint))] REG_DWORD_LITTLE_ENDIAN = 4, /// @@ -57,6 +61,7 @@ namespace Vanara.PInvoke REG_MULTI_SZ = 7, /// A 64-bit number. + [CorrespondingType(typeof(ulong))] REG_QWORD = 11, /// @@ -66,6 +71,7 @@ namespace Vanara.PInvoke /// Windows header files. /// /// + [CorrespondingType(typeof(ulong))] REG_QWORD_LITTLE_ENDIAN = 11, /// @@ -82,4 +88,46 @@ namespace Vanara.PInvoke /// Resource requirement list. REG_RESOURCE_REQUIREMENTS_LIST = 10, } +} + +namespace Vanara.Extensions +{ + /// Extension methods for registry types. + public static class RegistryTypeExt + { + /// Extract the value of this registry type from a pointer. + /// The registry type value. + /// The allocated memory pointer. + /// The size of the allocated memory. + /// The extracted value. + public static object GetValue(this Vanara.PInvoke.REG_VALUE_TYPE value, IntPtr ptr, uint size) + { + switch (value) + { + case PInvoke.REG_VALUE_TYPE.REG_DWORD: + return IntPtrConverter.Convert(ptr, size); + case PInvoke.REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN: + var data = IntPtrConverter.Convert(ptr, 4); + return unchecked((uint)((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3])); + case PInvoke.REG_VALUE_TYPE.REG_EXPAND_SZ: + return Environment.ExpandEnvironmentVariables(StringHelper.GetString(ptr)); + case PInvoke.REG_VALUE_TYPE.REG_LINK: + return new Uri(StringHelper.GetString(ptr)); + case PInvoke.REG_VALUE_TYPE.REG_MULTI_SZ: + return ptr.ToStringEnum(); + case PInvoke.REG_VALUE_TYPE.REG_QWORD: + return IntPtrConverter.Convert(ptr, size); + case PInvoke.REG_VALUE_TYPE.REG_SZ: + return StringHelper.GetString(ptr); + case PInvoke.REG_VALUE_TYPE.REG_RESOURCE_LIST: + case PInvoke.REG_VALUE_TYPE.REG_FULL_RESOURCE_DESCRIPTOR: + case PInvoke.REG_VALUE_TYPE.REG_RESOURCE_REQUIREMENTS_LIST: + case PInvoke.REG_VALUE_TYPE.REG_BINARY: + return IntPtrConverter.Convert(ptr, size); + default: + case PInvoke.REG_VALUE_TYPE.REG_NONE: + return ptr; + } + } + } } \ No newline at end of file