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.
我正在将Python包装器编写为C编译的stdcall(Windows)DLL(= mydll).我有2个版本的DLL-32位和64位.使用 windll.mydll
可以很好地使用64版本.对于DLL上的所有功能,除了可变参数 printf
之类的功能之外,对于相同的命令,该32版本的效果很好.
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屋!