.NET 3.x Чтение Windows key CD - C#
Формулировка задачи:
Пытаюсь вывести информацию о Windows key, пробую так:
вызываю
Но ничего не показывает
Как вывести ключ?
static string GetWindowsCDKey(bool bUseFormat)
{
int pos, start, value;
char[] sBuffer;
byte[] sRawData = (byte[])Registry.GetValue(
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
"DigitalProductId", null);
if(sRawData == null)
return null;
sBuffer = new char[32];
start = (bUseFormat) ? 28 : 24;
for(pos = 52; pos <= 66; pos++)
sRawData[pos - 52] = sRawData[pos];
for(pos = 24; pos >= 0; pos--){
value = 0;
for(int c = 14; c > -1; c--){
value = value * 256;
value ^= sRawData[c];
sRawData[c] = (byte)(value / 24);
value %= 24;
}
sBuffer[start--] = "BCDFGHJKMPQRTVWXY2346789"[value];
if(bUseFormat && pos > 0 && (pos % 5) == 0)
sBuffer[start--] = '-';
}
return new string(sBuffer);
}string sCDKey = GetWindowsCDKey(true);
Решение задачи: «.NET 3.x Чтение Windows key CD»
textual
Листинг программы
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
public static class Registry64Helper
{
enum RegWow64Options
{
None = 0,
KEY_WOW64_64KEY = 0x0100,
KEY_WOW64_32KEY = 0x0200
}
enum RegistryRights
{
ReadKey = 131097,
WriteKey = 131078
}
/// <summary>
/// Open a registry key using the Wow64 node instead of the default 32-bit node.
/// </summary>
/// <param name="parentKey">Parent key to the key to be opened.</param>
/// <param name="subKeyName">Name of the key to be opened</param>
/// <param name="writable">Whether or not this key is writable</param>
/// <returns></returns>
public static RegistryKey OpenSubKey(RegistryKey parentKey, string subKeyName, bool writable)
{
if (parentKey == null || GetRegistryKeyHandle(parentKey) == IntPtr.Zero)
{
return null;
}
int rights = (int)RegistryRights.ReadKey;
if (writable) rights = (int)RegistryRights.WriteKey;
int subKeyHandle;
int result = RegOpenKeyEx(GetRegistryKeyHandle(parentKey), subKeyName, 0, rights | (int)RegWow64Options.KEY_WOW64_64KEY, out subKeyHandle);
if (result != 0) return null;
return CreateRegistryKeyFromIntPtr((IntPtr)subKeyHandle, writable, false);
}
static IntPtr GetRegistryKeyHandle(RegistryKey registryKey)
{
Type registryKeyType = typeof(RegistryKey);
FieldInfo fieldInfo = registryKeyType.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);
SafeHandle handle = (SafeHandle)fieldInfo.GetValue(registryKey);
return handle.DangerousGetHandle();
}
static RegistryKey CreateRegistryKeyFromIntPtr(IntPtr hKey, bool writable, bool ownsHandle)
{
BindingFlags privateConstructors = BindingFlags.Instance | BindingFlags.NonPublic;
Type safeRegistryHandleType = typeof(SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");
Type[] safeRegistryHandleCtorTypes = { typeof(IntPtr), typeof(bool) };
ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(privateConstructors, null, safeRegistryHandleCtorTypes, null);
object safeHandle = safeRegistryHandleCtorInfo.Invoke(new object[] { hKey, ownsHandle });
Type registryKeyType = typeof(RegistryKey);
Type[] registryKeyConstructorTypes = { safeRegistryHandleType, typeof(bool) };
ConstructorInfo registryKeyCtorInfo = registryKeyType.GetConstructor(privateConstructors, null, registryKeyConstructorTypes, null);
return (RegistryKey)registryKeyCtorInfo.Invoke(new[] { safeHandle, writable });
}
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern int RegOpenKeyEx(IntPtr hKey, string subKey, int ulOptions, int samDesired, out int phkResult);
}