错误2在Win 8、8.1和7上读取SOFTWARE \ Microsoft \ Cryptography \ MachineGUID

时间:2018-11-01 11:15:41

标签: winapi windows-7 windows-8 registry windows-8.1

我收到错误2,试图从注册表中读取MachineGUID,这是我当前正在使用的代码:

LSTATUS l = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", 0, KEY_READ | KEY_WOW64_64KEY, &hResult);
CString csError;
if (l == ERROR_SUCCESS)
{
    l = RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGUID", RRF_RT_ANY | RRF_SUBKEY_WOW6464KEY, NULL, szGUID, &lSize);
    if (l != ERROR_SUCCESS)
    {
        l = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", 0, KEY_READ | KEY_WOW64_32KEY, &hResult);
        if (l == ERROR_SUCCESS)
        {
            l = RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGUID", RRF_RT_ANY | RRF_SUBKEY_WOW6432KEY, NULL, szGUID, &lSize);
            if (l != ERROR_SUCCESS)
            {
                l = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", 0, KEY_READ, &hResult);
                if (l == ERROR_SUCCESS)
                {
                    l = RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGUID", RRF_RT_ANY, NULL, szGUID, &lSize);
                    if (l != ERROR_SUCCESS)
                    {
                        csError.Format("Error %lu reading machine ID.", l);
                        MessageBox(csError);
                    }
                }
                else
                {
                    csError.Format("Error %lu opening machine ID with KEY_READ.", l);
                    MessageBox(csError);
                }
            }
        }
        else
        {
            csError.Format("Error %lu opening machine ID with KEY_READ | KEY_WOW64_32KEY.", l);
            MessageBox(csError);
        }
    }
}
else
{
    csError.Format("Error %lu opening machine ID with KEY_READ | KEY_WOW64_64KEY.", l);
    MessageBox(csError);
}

所有RegOpenKeyEx调用均出于调试目的;是的,我知道钥匙应该被关闭。我只是想看看开放访问是否存在问题,而没有任何问题。该代码一直到最内层的错误消息,即读取的错误消息。

该代码是使用VS 2017作为32位代码构建的。在Windows 10上可以正常工作。谁能告诉我问题出在哪里?

此代码应会产生问题。它使用MBCS而不是Unicode。

#include <windows.h>
#include <winreg.h>
#include <stdio.h>

int main()
{
  char szGUID[37];
  memset(szGUID, 0, 37);
  DWORD lSize = 37;

  ULONG ulResult = RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGUID", RRF_RT_ANY | RRF_SUBKEY_WOW6464KEY, NULL, szGUID, &lSize);
  if (ulResult != ERROR_SUCCESS)
  {
    lSize = 37;
    ulResult = RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGUID", RRF_RT_ANY | RRF_SUBKEY_WOW6432KEY, NULL, szGUID, &lSize);
    if (ulResult != ERROR_SUCCESS)
    {
      lSize = 37;
      ulResult = RegGetValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", "MachineGUID", RRF_RT_ANY, NULL, szGUID, &lSize);
      if (ulResult != ERROR_SUCCESS)
      {
        printf("Error %lu opening SOFTWARE\\Microsoft\\Cryptography\\MachineGUID.\n", ulResult);
      }
      else
        printf("Key SOFTWARE\\Microsoft\\Cryptography\\MachineGUID value %s\n", szGUID);
    }
    else
      printf("Key SOFTWARE\\Microsoft\\Cryptography\\MachineGUID (RRF_SUBKEY_WOW6432KEY) value %s\n", szGUID);
  }
  else
    printf("Key SOFTWARE\\Microsoft\\Cryptography\\MachineGUID (RRF_SUBKEY_WOW6464KEY) value %s\n", szGUID);
  return 0;
}

1 个答案:

答案 0 :(得分:0)

在Windows 10中引入了RRF_SUBKEY_WOW6432KEY标志。在以前的Windows版本中,它将被忽略。因此,当您在Windows 7和Windows 8上使用带有此标志的RegGetValue时,实际上是在读取没有SOFTWARE\\Wow6432Node\Microsoft\\Cryptography值的MachineGUID键。

您可以做的是使用"SOFTWARE\\Microsoft\\Cryptography"RegOpenKeyEx标志显式地打开KEY_WOW64_64KEY键,然后在该键上使用RegGetValue

下面的示例程序对此进行了演示。我尚未在真正的32位版本上对其进行过测试,但它应该按照this SO question正常工作。

#include <windows.h>
#include <winreg.h>
#include <stdio.h>

int main()
{
  char szGUID[37];
  memset(szGUID, 0, 37);
  DWORD lSize = 37;
  ULONG ulResult;

  HKEY hkey;
  ulResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Cryptography", 0, KEY_WOW64_64KEY + KEY_READ, &hkey);
  if (ulResult == 0)
  {
    ulResult = RegGetValue(hkey, "", "MachineGUID", RRF_RT_ANY, NULL, szGUID, &lSize);
    if (ulResult == 0)
    {
      printf("Key SOFTWARE\\Microsoft\\Cryptography\\MachineGUID value %s\n", szGUID);
    }
    else
    {
      printf("Error %lu during RegGetValue of MachineGUID", ulResult);
    }

    RegCloseKey(hkey);
  }
  else
  {
    printf("Error %lu opening HKEY_LOCAL_MACHINE, SOFTWARE\\Microsoft\\Cryptography\\MachineGUID.\n", ulResult);
  }
}