1

I'm trying to read MachineGuid key from windows registry using QSettings.

The address to that key is

"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography".

I'm using the QSettings with QSettings::Native flag as follows.

QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography",QSettings::NativeFormat);

I can see all the subfolders and their keys and the value of MachineGuid from regedit.exe but the value function of QSettings does not seem to be working correctly.

the results are as follows:

settings.value("MachineGuid").toString();

returns empty QString.

settings.childGroups();

returns all the subfolders of Cryptography folder correctly.

settings.childKeys();

returns an empty QStringList

settings.allKeys();

returns all the keys inside Cryptography folder including the keys inside of all subfolders except for MachineGuid which is placed inside Cryptography.

I'm using Qt 5.7.1 built statically using Visual Studio 2015 on Windows 10.

I have tried codes that use Window.h header and I have successfully extracted the value but the problem with this approach is that I have to add lots of DLLs to my released software.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
mostafaTmj
  • 363
  • 4
  • 12

1 Answers1

4

For accessing 64 Bit Windows OS with 32 Bit compiled code, the right format would be "Registry64Format" and if accessing 32 Bit OS from 64 compiler the the right format is "Registry32Format" So, in my case, after setting format to 64 bit, the key could be fetched.

to read key "MachineGuid" on 64 bit OS with 32 bit Compiled code;

QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography",
QSettings::Registry64Format);
auto key = settings.childKeys().at(0);
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • Hey Mohammad thanks for the answer but i think you have to see for yourself that if you click on cryptography on MachineGuid appears the right side of your regedit it is not a group. The problem is that Cryptography contains a key and bunch of groups as children – mostafaTmj May 06 '18 at 05:09
  • @mostafaTmj No I don't have that key in my system – Mohammad Kanan May 06 '18 at 05:26
  • oh... I thought every windows machine has that key as mentioned here https://stackoverflow.com/questions/99880/generating-a-unique-machine-id – mostafaTmj May 06 '18 at 05:30
  • and here is another example of the MachineGuid with a picture at the end https://www.nextofwindows.com/the-best-way-to-uniquely-identify-a-windows-machine – mostafaTmj May 06 '18 at 05:34
  • Oh sorry, I could see it now! but I know the problem anyway! – Mohammad Kanan May 06 '18 at 06:05
  • @mostafaTmj, sorry for that but I updated my answer: the idea is QSettings needs to know you are editing 64 bit registry (or 32 bit if compiler is 64) in that case `NativeFormat` wont help, you need to use `Registry64Format` – Mohammad Kanan May 06 '18 at 06:16
  • Great and sorry again @mostafaTmj I didn't notice it because I probably used `regedt32` instead if `regedit` ! – Mohammad Kanan May 06 '18 at 07:05