windll ctypes 从 python 2.7 调用可变参数 c 函数在 win64 中工作但在 win32 中不工作

windll ctypes call variadic c function from python 2.7 works in win64 but not in win32

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

我正在为 C 编译的标准调用 (Windows) DLL (= mydll) 编写 python 包装器。我有 2 个版本的 DLL - 32 位和 64 位。 64 位版本使用 windll.mydll 效果很好。 32 版本对 DLL 上的所有函数使用相同的命令效果很好,可变参数 printf 类函数除外。

当运行mydll.myvarfunc("Hello")

我明白了 ValueError: Procedure probably called with too many arguments (4 bytes in excess)

有没有不涉及更改可变参数函数的 C 代码的解决方法?

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

解决此问题的一种方法:

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