Search code examples
pythonwindowswinreg

Trying to get MachineGuid through _winreg but not working


I can see the value in my registry editor and the path is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography, but can't get the value with the following codes:

import _winreg

key = _winreg.OpenKey(
    _winreg.HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Microsoft\\Cryptography"
    )

result = _winreg.QueryValueEx(key, "MachineGuid")

print result

I got "The system cannot find the file specified", which is confusing because it's right there.

Well, there's no problem retrieving other values with the almost exact same code:

key = _winreg.OpenKey(
    _winreg.HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Microsoft\\COM3"
    )


result = _winreg.QueryValueEx(key, "BuildType")

print result

The output is: (u'Free', 1)

Which part am I doing wrong? What can I do to get this MachineGuid?


Solution

  • Your code is working fine on a 32bit Windows, if you want it to also run on a 64bit Windows, try this :

    key = _winreg.OpenKey(
        _winreg.HKEY_LOCAL_MACHINE,
        "SOFTWARE\\Microsoft\\Cryptography",
        0,
        _winreg.KEY_READ | _winreg.KEY_WOW64_64KEY
    )
    
    result = _winreg.QueryValueEx(key, "MachineGuid")
    print result
    

    Reference : Change 64bit Registry from 32bit Python