.NET 3.x Чтение Windows key CD - C#

Узнай цену своей работы

Формулировка задачи:

Пытаюсь вывести информацию о Windows key, пробую так:
Листинг программы
  1. static string GetWindowsCDKey(bool bUseFormat)
  2. {
  3. int pos, start, value;
  4. char[] sBuffer;
  5. byte[] sRawData = (byte[])Registry.GetValue(
  6. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
  7. "DigitalProductId", null);
  8. if(sRawData == null)
  9. return null;
  10. sBuffer = new char[32];
  11. start = (bUseFormat) ? 28 : 24;
  12. for(pos = 52; pos <= 66; pos++)
  13. sRawData[pos - 52] = sRawData[pos];
  14. for(pos = 24; pos >= 0; pos--){
  15. value = 0;
  16. for(int c = 14; c > -1; c--){
  17. value = value * 256;
  18. value ^= sRawData[c];
  19. sRawData[c] = (byte)(value / 24);
  20. value %= 24;
  21. }
  22. sBuffer[start--] = "BCDFGHJKMPQRTVWXY2346789"[value];
  23. if(bUseFormat && pos > 0 && (pos % 5) == 0)
  24. sBuffer[start--] = '-';
  25. }
  26. return new string(sBuffer);
  27. }
вызываю
Листинг программы
  1. string sCDKey = GetWindowsCDKey(true);
Но ничего не показывает Как вывести ключ?

Решение задачи: «.NET 3.x Чтение Windows key CD»

textual
Листинг программы
  1. using System.Reflection;
  2. using System.Runtime.InteropServices;
  3. using Microsoft.Win32;
  4. using Microsoft.Win32.SafeHandles;
  5.  
  6. public static class Registry64Helper
  7. {
  8.     enum RegWow64Options
  9.     {
  10.         None = 0,
  11.         KEY_WOW64_64KEY = 0x0100,
  12.         KEY_WOW64_32KEY = 0x0200
  13.     }
  14.  
  15.     enum RegistryRights
  16.     {
  17.         ReadKey = 131097,
  18.         WriteKey = 131078
  19.     }
  20.  
  21.     /// <summary>
  22.     /// Open a registry key using the Wow64 node instead of the default 32-bit node.
  23.     /// </summary>
  24.     /// <param name="parentKey">Parent key to the key to be opened.</param>
  25.     /// <param name="subKeyName">Name of the key to be opened</param>
  26.     /// <param name="writable">Whether or not this key is writable</param>
  27.     /// <returns></returns>
  28.     public static RegistryKey OpenSubKey(RegistryKey parentKey, string subKeyName, bool writable)
  29.     {
  30.         if (parentKey == null || GetRegistryKeyHandle(parentKey) == IntPtr.Zero)
  31.         {
  32.             return null;
  33.         }
  34.  
  35.         int rights = (int)RegistryRights.ReadKey;
  36.         if (writable) rights = (int)RegistryRights.WriteKey;
  37.  
  38.         int subKeyHandle;
  39.         int result = RegOpenKeyEx(GetRegistryKeyHandle(parentKey), subKeyName, 0, rights | (int)RegWow64Options.KEY_WOW64_64KEY, out subKeyHandle);
  40.         if (result != 0) return null;
  41.  
  42.         return CreateRegistryKeyFromIntPtr((IntPtr)subKeyHandle, writable, false);
  43.     }
  44.  
  45.     static IntPtr GetRegistryKeyHandle(RegistryKey registryKey)
  46.     {
  47.         Type registryKeyType = typeof(RegistryKey);
  48.         FieldInfo fieldInfo = registryKeyType.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);
  49.         SafeHandle handle = (SafeHandle)fieldInfo.GetValue(registryKey);
  50.         return handle.DangerousGetHandle();
  51.     }
  52.  
  53.     static RegistryKey CreateRegistryKeyFromIntPtr(IntPtr hKey, bool writable, bool ownsHandle)
  54.     {
  55.         BindingFlags privateConstructors = BindingFlags.Instance | BindingFlags.NonPublic;
  56.         Type safeRegistryHandleType = typeof(SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle");
  57.         Type[] safeRegistryHandleCtorTypes = { typeof(IntPtr), typeof(bool) };
  58.         ConstructorInfo safeRegistryHandleCtorInfo = safeRegistryHandleType.GetConstructor(privateConstructors, null, safeRegistryHandleCtorTypes, null);
  59.         object safeHandle = safeRegistryHandleCtorInfo.Invoke(new object[] { hKey, ownsHandle });
  60.  
  61.         Type registryKeyType = typeof(RegistryKey);
  62.         Type[] registryKeyConstructorTypes = { safeRegistryHandleType, typeof(bool) };
  63.         ConstructorInfo registryKeyCtorInfo = registryKeyType.GetConstructor(privateConstructors, null, registryKeyConstructorTypes, null);
  64.         return (RegistryKey)registryKeyCtorInfo.Invoke(new[] { safeHandle, writable });
  65.     }
  66.  
  67.     [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
  68.     public static extern int RegOpenKeyEx(IntPtr hKey, string subKey, int ulOptions, int samDesired, out int phkResult);
  69. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.25 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы