Python 2.6 - 我无法使用 _winreg.SetValueEx() 将大于 0x7fffffff 的双字写入注册表

发布于 2024-08-24 06:02:51 字数 1112 浏览 1 评论 0原文

使用 regedit.exe 我在注册表中手动创建了一个名为
的注册表项 HKEY_CURRENT_USER/00_Just_a_Test_Key
并创建了两个双字值
dword_test_1 和 dword_test_2
我正在尝试使用以下程序将一些值写入这两个键

import _winreg

aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
aKey = _winreg.OpenKey(aReg, r"00_Just_a_Test_Key", 0, _winreg.KEY_WRITE)

_winreg.SetValueEx(aKey,"dword_test_1",0, _winreg.REG_DWORD, 0x0edcba98) 
_winreg.SetValueEx(aKey,"dword_test_2",0, _winreg.REG_DWORD, 0xfedcba98) 

_winreg.CloseKey(aKey)
_winreg.CloseKey(aReg)  

,我可以写入第一个键 dword_test_1,但是当我尝试写入第二个键时,我收到以下消息

Traceback (most recent call last):
  File "D:/src/registry/question.py", line 7, in <module>
    _winreg.SetValueEx(aKey,"dword_test_2",0, _winreg.REG_DWORD, 0xfedcba98)
ValueError: Could not convert the data to the specified type.

如何写入第二个值 0xfedcba98 或任何值大于 0x7fffffff
作为双字值?

最初,我正在编写脚本来打开或关闭“我的文档”图标,方法是将“0xf0500174”写入隐藏或“0xf0400174”显示图标到 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CLSID{450D8FBA-AD25 -11D0-98A8-0800361B1103}\ShellFolder]

using regedit.exe I have manually created a key in registry called
HKEY_CURRENT_USER/00_Just_a_Test_Key
and created two dword values
dword_test_1 and dword_test_2
I am trying to write some values into those two keys using following program

import _winreg

aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
aKey = _winreg.OpenKey(aReg, r"00_Just_a_Test_Key", 0, _winreg.KEY_WRITE)

_winreg.SetValueEx(aKey,"dword_test_1",0, _winreg.REG_DWORD, 0x0edcba98) 
_winreg.SetValueEx(aKey,"dword_test_2",0, _winreg.REG_DWORD, 0xfedcba98) 

_winreg.CloseKey(aKey)
_winreg.CloseKey(aReg)  

I can write into the first key, dword_test_1, but when I attempt to write into the second, I get following message

Traceback (most recent call last):
  File "D:/src/registry/question.py", line 7, in <module>
    _winreg.SetValueEx(aKey,"dword_test_2",0, _winreg.REG_DWORD, 0xfedcba98)
ValueError: Could not convert the data to the specified type.

How do I write the second value 0xfedcba98, or any value greater than 0x7fffffff
as a dword value?

Originally I was writing script to switch the "My documents" icon on or off by writing "0xf0500174" to hide or "0xf0400174" to display the icon into [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\CLSID{450D8FBA-AD25-11D0-98A8-0800361B1103}\ShellFolder]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-08-31 06:02:51

该函数很可能需要一个在有符号 C 整数限制内的 int,因此您需要在传递给该函数之前减去 0x100000000

是的,理想情况下这可以在绑定中解决。不幸的是,有人放过了这一点。

Most likely the function expects an int within the limits of a signed C integer, so you'll need to subtract 0x100000000 before passing to the function.

Yes, ideally this would be solved in the bindings. Unfortunately someone let this one slide.

三岁铭 2024-08-31 06:02:51

我通过以下方式解决了问题

import _winreg

def complement(n,radix=32):
    if n < (1<<(radix-1)) : return n   # n is less than 0x80000000 and we do not do anything
    else : return n - (1<<radix)       # n is greater than 0x80000000 and we have to convert it
    # (1<<31) can be written in binary as 1 followed by 31 zeroes - that is 0x80000000
    # n - (1<<radix) is how to get the representation of the number as a signed dword.
    # See http://stackoverflow.com/questions/1604464/twos-complement-in-python
    # for explanation

aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
aKey = _winreg.OpenKey(aReg, r"00_Just_a_Test_Key", 0, _winreg.KEY_WRITE)

_winreg.SetValueEx(aKey,"dword_test_1",0, _winreg.REG_DWORD, complement(0x0edcba98)) 
_winreg.SetValueEx(aKey,"dword_test_2",0, _winreg.REG_DWORD, complement(0xfedcba98)) 

_winreg.CloseKey(aKey)
_winreg.CloseKey(aReg)

I have solved the problem the following way

import _winreg

def complement(n,radix=32):
    if n < (1<<(radix-1)) : return n   # n is less than 0x80000000 and we do not do anything
    else : return n - (1<<radix)       # n is greater than 0x80000000 and we have to convert it
    # (1<<31) can be written in binary as 1 followed by 31 zeroes - that is 0x80000000
    # n - (1<<radix) is how to get the representation of the number as a signed dword.
    # See http://stackoverflow.com/questions/1604464/twos-complement-in-python
    # for explanation

aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
aKey = _winreg.OpenKey(aReg, r"00_Just_a_Test_Key", 0, _winreg.KEY_WRITE)

_winreg.SetValueEx(aKey,"dword_test_1",0, _winreg.REG_DWORD, complement(0x0edcba98)) 
_winreg.SetValueEx(aKey,"dword_test_2",0, _winreg.REG_DWORD, complement(0xfedcba98)) 

_winreg.CloseKey(aKey)
_winreg.CloseKey(aReg)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文