Search code examples
python-3.xregistrywinreg

Why registry value doesn't match in winreg in Python 3 on Windows?



Hello everybody!

I am coding small python3 project. In my code, there's a function to check whether a user has installed specific DirectX version or not.

That function is as below.

def check_directx():
    try:
        reg_obj = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\DirectX')
    except FileNotFoundError:
        return False

    dx_value_str = EnumValue(reg_obj, 1)[1][0:4]
    CloseKey(reg_obj)

    if dx_value_str != "4.09":
        return False

    return True

I wanted to test this function so I changed the original version value(4.09.00.0904) to 4.07.00.0904 in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\DirectX. But it didn't work. variable dx_value_str's value was still 4.09.00.0904.

I searched registry and found that there's a same key, value in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\DirectX. I changed the version value of here, variable dx_value_str's value has been changed at last.

As you see, I opened the key in 'SOFTWARE\Microsoft\DirectX' But it brought the value in 'SOFTWARE\Wow6432Node\Microsoft\DirectX'.

Is this situation all right? or there's something wrong in my code?

Please help me!


Solution

  • This problem is because you might be using 32 bit python,try this:

    import winreg
    reg_obj = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\DirectX',0, (winreg.KEY_WOW64_64KEY+ winreg.KEY_READ))
    dx_value_str = winreg.EnumValue(reg_obj, 1)[1][0:4]
    print(dx_value_str)
    winreg.CloseKey(reg_obj)