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

血红的双手。 提交于 2020-01-30 08:19:29

问题


I'm using Python 2.7 on Windows 10-32 and Windows 10-64.

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.

When running mydll.myvarfunc("Hello")

I get 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?


回答1:


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


来源:https://stackoverflow.com/questions/50386340/windll-ctypes-call-variadic-c-function-from-python-2-7-works-in-win64-but-not-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!