首页 > 解决方案 > 来自python 2.7的windll ctypes调用可变参数c函数在win64中有效,但在win32中无效

问题描述

我在 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: Procedure probably called with too many arguments (4 bytes in excess)

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

标签: pythonwindowsctypes32bit-64bitvariadic-functions

解决方案


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

解决此问题的一种方法:

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

推荐阅读