title: Getting windows serial number (MachineGuid) in both x86 and x64 architectures
body: <p>I´m currently using the following C++ code to get the <code>MachineGuid</code> from the windows registry and use that information for my licensing algorithm:</p>
  
  <pre><code>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, &amp;hKey) != ERROR_SUCCESS)
      throw std::runtime_error("Could not open registry key");
  
  DWORD type;
  DWORD cbData;
  
  if (RegQueryValueEx(hKey, name.c_str(), NULL, &amp;type, NULL, &amp;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&lt;LPBYTE&gt;(&amp;value[0]), &amp;cbData) != ERROR_SUCCESS)
  {
      RegCloseKey(hKey);
      throw "Could not read registry value";
  }
  
  RegCloseKey(hKey);
  </code></pre>
  
  <p>This works pretty well on a x86 system (32 bit). Now I´ve migrated the whole code to x64 (64bit) Windows and the <code>RegQueryValueEx</code> call is returning error.</p>
  
  <p>Among some other posts, <a href="https://stackoverflow.com/questions/14213020/getting-windows-serial-number-was-getting-machineguid-from-registry">this link</a> explains very clearly why this does not work on 64bit machines, and offers an alternative for both 32bit and 64bit using the <code>ManagementObject</code> class from <code>System.Management.dll</code>. The problem is that this solution works on C#, not on C++. I can´t find out a C++ equivalent of the <code>ManagementObject</code> class.</p>
  
  <p>So, what is the correct solution for the problem: Getting the window serial number (<code>MachineGuid</code>) on both x86 and x64 machines using C++.</p>
  
  <p>Thanks for helping.</p>