Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot pass parameters to dll with ctypes (Python)

Tags:

python

dll

ctypes

Having some problems using ctypes

I have a testdll with following interface

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);
}

I also have a .def file so i have "real" names

LIBRARY "MathFuncsDll"
EXPORTS

 Add
 Subtract
 Multiply
 Divide

I can load and access the function from the dll via ctype but I cannot pass the parameter, see 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)
>>> 

But I can the Function Add without parameters?!?!?!?!

>>> x.Add()
2619260

Can somebody point me in the right direction? I think forget something obvious because I can call function from other dll (eg. kernel32)

like image 940
nobs Avatar asked Feb 20 '23 21:02

nobs


1 Answers

ctypes assumes int and pointer types for parameters and int for return values unless you specify otherwise. Exported functions also typically default to the C calling convention (CDLL in ctypes), not WinDLL. Try this:

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)

Output

3.5
like image 146
Mark Tolonen Avatar answered Mar 08 '23 05:03

Mark Tolonen