[英]Problems about python ctypes module
Just trying to mess around and learn about python ctypes according to the official documentation at https://docs.python.org/3.8/library/ctypes.html
一切正常,直到:
当您使用 cdecl 调用约定调用 stdcall function 时会引发 ValueError ,反之亦然:
>>> cdll.kernel32.GetModuleHandleA(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>
>>> windll.msvcrt.printf(b"spam")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
从官方文档中引用,而我得到的是:
>>> cdll.kernel32.GetModuleHandleA(None)
1374486528
>>> windll.msvcrt.printf(b"spam")
4
根据那些 MS 文档,似乎这些 function 调用工作得很好
更重要的是,我还试图弄乱参数编号以引发 ValueError,但这就是我得到的:
>>> cdll.kernel32.GetModuleHandleA(None,0,0)
1374486528
>>> windll.kernel32.GetModuleHandleA(0,0,0)
1374486528
>>> windll.kernel32.GetModuleHandleA()
0
>>> cdll.kernel32.GetModuleHandleA()
0
似乎最后两个 function 调用确实返回 null 因为有错误,但没有值错误异常。 正如文档示例所示,我得到的唯一错误是 OSError。
谁能解释一下? 我使用 conda 创建虚拟环境,并在 python 3.6.12 和 python 3.8.5 中测试这些代码。
顺便说一句,根据文档:“当您使用 cdecl 调用约定调用 stdcall function 时会引发 ValueError,反之亦然”,我想知道“使用 cdecl 调用约定调用 stdcall function”到底是什么意思? 也许只是通过提供不同数量的 arguments 而不是所需的 function?
__stdcall
和_cdecl
在 64 位编译器上没有区别。 只有一种调用约定,符号被忽略,WinDLL 和 CDLL 都可以工作。 32 位代码很重要,必须使用正确的代码。
如果您希望脚本在 32 位和 64 位 Python 上正常工作,您仍应在脚本中使用适当的 WinDLL 或 CDLL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.