Mendes
Mendes

Reputation: 18451

Getting windows serial number (MachineGuid) in both x86 and x64 architectures

I´m currently using the following C++ code to get the MachineGuid from the windows registry and use that information for my licensing algorithm:

std::wstring key = L"SOFTWARE\\Microsoft\\Cryptography";
std::wstring name = L"MachineGuid";

HKEY hKey;

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, key.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
    throw std::runtime_error("Could not open registry key");

DWORD type;
DWORD cbData;

if (RegQueryValueEx(hKey, name.c_str(), NULL, &type, NULL, &cbData) != ERROR_SUCCESS)
{
    RegCloseKey(hKey);
    throw std::runtime_error("Could not read registry value");
}

if (type != REG_SZ)
{
    RegCloseKey(hKey);
    throw "Incorrect registry value type";
}

std::wstring value(cbData/sizeof(wchar_t), L'\0');
if (RegQueryValueEx(hKey, name.c_str(), NULL, NULL, reinterpret_cast<LPBYTE>(&value[0]), &cbData) != ERROR_SUCCESS)
{
    RegCloseKey(hKey);
    throw "Could not read registry value";
}

RegCloseKey(hKey);

This works pretty well on a x86 system (32 bit). Now I´ve migrated the whole code to x64 (64bit) Windows and the RegQueryValueEx call is returning error.

Among some other posts, this link explains very clearly why this does not work on 64bit machines, and offers an alternative for both 32bit and 64bit using the ManagementObject class from System.Management.dll. The problem is that this solution works on C#, not on C++. I can´t find out a C++ equivalent of the ManagementObject class.

So, what is the correct solution for the problem: Getting the window serial number (MachineGuid) on both x86 and x64 machines using C++.

Thanks for helping.

Upvotes: 5

Views: 4752

Answers (1)

Soonts
Soonts

Reputation: 21936

Add KEY_WOW64_64KEY bit to your RegOpenKeyEx argument. Like this:

RegOpenKeyEx( HKEY_LOCAL_MACHINE, key.c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &hKey )

The documentation says it’s ignored on 32-bit OSes, so you don’t even need to detect WOW64.

P.S. I don’t recommend WMI, it’s too slow. I currently have i5-4460 CPU, 16GB RAM, relatively fast SSD, and yet WMI takes 1-2 seconds to initialize and run even a simple query.

Upvotes: 6

Related Questions