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.
I'm using Python 2.7 on Windows 10-32 and Windows 10-64.
我正在为 C 编译的 stdcall (Windows) DLL (= mydll) 编写一个 python 包装器.我有 2 个版本的 DLL - 32 位和 64 位.64 版本使用 windll.mydll
效果很好.32 版本对 DLL 上的所有函数使用相同的命令效果很好,除了类似可变参数的 printf
函数.
I'm writing a python wrapper to a C compiled stdcall (Windows) DLL (= mydll). I have 2 versions of the DLL - 32 and 64 bit.
The 64 version works great using windll.mydll
.
The 32 version works great using the same command for all functions on the DLL, except for variadic printf
-like functions.
运行时mydll.myvarfunc("Hello")
我明白了ValueError: 调用的过程可能参数过多(超过 4 个字节)
有没有不涉及更改可变参数函数的 C 代码的方法?
Is there a way around this that does not involve changing the C code for the variadic functions?
推荐答案
在 Win64 上,只有一个 ABI,所以 WinDLL 和 CDLL 没有区别.在 Win32 上,可变参数函数始终是 __cdecl
,因此 WinDLL 使用了错误的调用约定.
On Win64, there is only one ABI so WinDLL and CDLL make no difference. On Win32, variadic functions are always __cdecl
so WinDLL is using the wrong calling convention.
解决此问题的一种方法:
One way to work around this:
import ctypes
stdcall_func = ctypes.WinDLL('mydll').stdcall_func
cdecl_func = ctypes.CDLL('mydll').cdecl_func
这篇关于Windll ctypes 调用来自 python 2.7 的可变参数 c 函数在 win64 中有效,但在 win32 中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!