Search code examples
pythonpython-3.xwinreg

How to get value of REG_DWORD variable from registry


I've created a variable in the Windows registry (via regedit), and want to get the value of my variable which has a REG_DWORD type. I use this code to get the value:

def get_DWORD_val(): 
    from winreg import ConnectRegistry, HKEY_LOCAL_MACHINE, OpenKey, QueryValue, REG_EXPAND_SZ, REG_SZ
    try:
           root = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
           print("---1")
           root_key = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Python', 0, KEY_READ)
           print("---2")
           [Pathname,regtype]=(QueryValue(root_key,"Ver_Tokenlog"))

    except WindowsError:
        return ["Error"]

    return Pathname

Output :

---1
---2
['Error']

This error is thrown:

winerror 2 the system cannot find the file specified

Solution

  • I guess you meant QueryValueEx:

    def get_DWORD_val(): 
        from winreg import ConnectRegistry, HKEY_LOCAL_MACHINE, OpenKey, QueryValueEx, QueryValue, REG_EXPAND_SZ, REG_SZ, KEY_READ
    
        root = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
        print("---1")
        root_key = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Python', 0, KEY_READ)
        print("---2")
        Pathname,regtype = QueryValueEx(root_key, "Ver_Tokenlog")
        print(Pathname)
        print(regtype)
    
    
    get_DWORD_val()
    

    Output is:

    ---1
    ---2
    256
    4
    

    Reg values: enter image description here