Python ctypes参数错误 [英] Python ctypes argument errors
问题描述
我的代码是:
import ctypes
import string
nDLL = ctypes.WinDLL('test.dll')
func = nDLL ['haloshg_add']
func.restype = ctypes.c_double
func.argtypes =(ctypes.c_double,ctypes.c_double)
print(func(5.0,5.0))
它返回错误的行称为func:
ValueError:可能使用太多参数调用过程(超出8个字节)
我做错了什么?谢谢。
您可能会收到调用约定。我猜你有一个C函数声明为这样:
double haloshg_add(double d1,double s2)
{
返回d1 + d2;
}
这将默认使用C调用约定。最简单的方法是在ctypes代码中更改调用约定:
nDLL = ctypes.CDLL('test.dll' )
如果要将C代码中的调用约定更改为 stdcall
(以匹配 ctypes.WinDLL
)然后你会这样做:
double __stdcall haloshg_add(double d1,double s2)
无论你做什么,只做其中一项变更。如果你这样做都会有反向的失败!
如果是我,我只是改变Python代码来使用C调用约定(使用 CDLL
)。这种变化影响最小。
I wrote a test dll in C++ to make sure things work before I start using a more important dll that I need. Basically it takes two doubles and adds them, then returns the result. I've been playing around and with other test functions I've gotten returns to work, I just can't pass an argument due to errors. My code is:
import ctypes
import string
nDLL = ctypes.WinDLL('test.dll')
func = nDLL['haloshg_add']
func.restype = ctypes.c_double
func.argtypes = (ctypes.c_double,ctypes.c_double)
print(func(5.0,5.0))
It returns the error for the line that called "func":
ValueError: Procedure probably called with too many arguments (8 bytes in excess)
What am I doing wrong? Thanks.
You probably got the calling conventions mixed up. I'm guessing you have a C function declared something like this:
double haloshg_add(double d1, double s2)
{
return d1+d2;
}
This will use the C calling convention by default. The simplest approach would be to change the calling convention in your ctypes code:
nDLL = ctypes.CDLL('test.dll')
If you wanted to change the calling convention in the C code to stdcall
(to match ctypes.WinDLL
) then you would do this:
double __stdcall haloshg_add(double d1, double s2)
Whatever you do, only do one of these changes. If you do both you'll have the reverse failure!
If it were me, I'd just change the Python code to use C calling convention (use CDLL
). That change has the least impact.
这篇关于Python ctypes参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!