python - Windll ctypes 从 python 2.7 调用可变参数 c 函数在 win64 中有效,但在 win32 中无效

标签 python windows ctypes 32bit-64bit variadic-functions

我在 Windows 10-32 和 Windows 10-64 上使用 Python 2.7。

我正在为 C 编译的 stdcall (Windows) DLL (= mydll) 编写一个 python 包装器。我有 2 个版本的 DLL - 32 位和 64 位。 64 版本使用 windll.mydll 效果很好。 32 版本可以很好地使用相同的命令来处理 DLL 上的所有函数(除了可变参数 printf 之类的函数)。

运行时mydll.myvarfunc("Hello")

我明白了 ValueError:调用过程可能使用了太多参数(超出 4 个字节)

有没有一种方法可以解决这个问题,而不涉及更改可变参数函数的 C 代码?

最佳答案

在 Win64 上,只有一个 ABI,因此 WinDLL 和 CDLL 没有区别。在 Win32 上,可变参数函数始终为 __cdecl,因此 WinDLL 使用了错误的调用约定。

解决此问题的一种方法:

import ctypes
stdcall_func = ctypes.WinDLL('mydll').stdcall_func
cdecl_func = ctypes.CDLL('mydll').cdecl_func

关于python - Windll ctypes 从 python 2.7 调用可变参数 c 函数在 win64 中有效,但在 win32 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386340/

相关文章:

python - 使用 ctypes (Python->C++) 提交 'int' 和 'string' 数组

python - 在 PyTorch 中实现快速密集特征提取

python - Python 中的文本移位函数

python - Django 初始数据 fixture 安装顺序

windows - 批量选择菜单?

windows - 使用 Chef 的多个 tomcat 实例

如果文件已存在,Python 会跳过函数

windows - 如何在不重新启动的情况下更新 Windows 资源管理器的 shell 扩展

python - 是否存在 UCL 的 Python 实现或接口(interface)?

python - 清理 python 切片?