[英]Cannot pass parameters to dll with ctypes (Python)
使用 ctypes 時遇到一些問題
我有一個帶有以下界面的testdll
extern "C"
{
// Returns a + b
double Add(double a, double b);
// Returns a - b
double Subtract(double a, double b);
// Returns a * b
double Multiply(double a, double b);
// Returns a / b
double Divide(double a, double b);
}
我也有一個.def文件,所以我有“真實”的名字
LIBRARY "MathFuncsDll"
EXPORTS
Add
Subtract
Multiply
Divide
我可以通過 ctype 從 dll 加載和訪問 function 但我無法傳遞參數,請參見 python output
>>> from ctypes import *
>>> x=windll.MathFuncsDll
>>> x
<WinDLL 'MathFuncsDll', handle 560000 at 29e1710>
>>> a=c_double(2.12)
>>> b=c_double(3.4432)
>>> x.Add(a,b)
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
x.Add(a,b)
ValueError: Procedure probably called with too many arguments (16 bytes in excess)
>>>
但是我的Function可以不帶參數添加????!?!
>>> x.Add()
2619260
有人能指出我正確的方向嗎? 我想忘記一些明顯的事情,因為我可以從其他 dll(例如 kernel32)調用 function
ctypes
假定參數為int
和pointer
類型,返回值假定為int
,除非您另有說明。 導出的函數通常也默認為 C 調用約定(ctypes 中的 CDLL),而不是 WinDLL。 試試這個:
from ctypes import *
x = CDLL('MathFuncsDll')
add = x.Add
add.restype = c_double
add.argtypes = [c_double,c_double]
print add(1.0,2.5)
3.5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.