I'm getting an error 2 trying to read the MachineGUID from the registry here is the code that I'm currently using:
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);
}
All of the RegOpenKeyEx calls were put in for debugging purposes; yes I know the key should be closed. I just wanted to see if there would be an issue with the open access and there is none. The code drops all the way through to the innermost error message, the read error message.
The code was built using VS 2017 as 32-bit code. This works fine on windows 10. Can anyone tell me what the problem is?
This code should generate the problem. It uses MBCS rather than 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;
}