简体   繁体   English

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

I'm using Python 2.7 on Windows 10-32 and Windows 10-64. 我在Windows 10-32和Windows 10-64上使用Python 2.7。

I'm writing a python wrapper to a C compiled stdcall (Windows) DLL (= mydll). 我正在将Python包装器编写为C编译的stdcall(Windows)DLL(= mydll)。 I have 2 versions of the DLL - 32 and 64 bit. 我有2个版本的DLL-32位和64位。 The 64 version works great using windll.mydll . 64版本使用windll.mydll效果很好。 The 32 version works great using the same command for all functions on the DLL, except for variadic printf -like functions. 32版本对DLL上的所有功能使用相同的命令非常有效,但可变参数printf类的功能除外。

When running mydll.myvarfunc("Hello") 运行mydll.myvarfunc("Hello")

I get ValueError: Procedure probably called with too many arguments (4 bytes in excess) 我收到ValueError: Procedure probably called with too many arguments (4 bytes in excess)

Is there a way around this that does not involve changing the C code for the variadic functions? 有没有一种解决方法,该方法不涉及更改可变参数函数的C代码?

On Win64, there is only one ABI so WinDLL and CDLL make no difference. 在Win64上,只有一个ABI,因此WinDLL和CDLL没有区别。 On Win32, variadic functions are always __cdecl so WinDLL is using the wrong calling convention. 在Win32上,可变参数函数始终为__cdecl因此WinDLL使用了错误的调用约定。

One way to work around this: 解决此问题的一种方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM