如何从注册表中获取REG_DWORD变量的值

2024-06-17 12:39:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在Windows注册表中创建了一个变量(通过regedit),并希望获取具有REG_DWORD类型的变量的值。我使用以下代码获取值:

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

输出:

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

将引发此错误:

winerror 2 the system cannot find the file specified

Tags: keyreturnlocalerrorrootmachineregprint
1条回答
网友
1楼 · 发布于 2024-06-17 12:39:11

我猜你指的是QueryValuex:

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()

输出为:

 -1
 -2
256
4

注册值: enter image description here

相关问题 更多 >