[ctypes-users] Using python ctypes to call a c function in a dll that returns a pointer to a struct
Brought to you by:
theller
|
From: John M. <te...@bl...> - 2012-02-24 18:39:27
|
Hello,
I am new to ctypes and I am very excited about its capabilities.
Unfortunately, I have not found any examples
that have helped me solve my issue so I thought I would post it here.
I have a C-DLL 'userlib_win64.dll' as a test
function that multiplies 2 numbers and returns a pointer to a structure
which as elements has the inputs and their
product as follows,
typedef struct {
double a;
double b;
double c;
} MULT;
_declspec(dllexport) MULT
*multNumbers(_int32,_int32);
MULT *multNumbers(_int32 a,_int32 b) {
MULT *m = malloc(sizeof(MULT));
m->a = (double)
a;
m->b = (double) b;
m->c = m->a * m->b;
return(m);
}
I am using a python script called
'mult_test.py'to call this function as follows,
from ctypes import *
ulib = cdll.userlib_win64
mult = ulib.multNumbers
class MULT(Structure): pass
MULT._fields_ = [
("a", c_double),
("b", c_double),
("c", c_double)]
mult.argtypes = [c_int,c_int]
mult.restype = pointer(MULT)
#mult(1,2)
p = MULT()
p.a = 3.
p.b = 2.
p.c = p.a*p.b
print p.a, p.b, p.c
q = mult(4,5)
I receive an error message as follows,
D:\codes\Simlib-1.0\bin\lib>mult_test
Traceback (most recent call last):
File "D:\codes\Simlib-1.0\bin\lib\mult_test.py", line 10, in <module>
mult.restype = pointer(MULT)
TypeError: _type_ must have storage info
If I change line
10 to,
mult.restype = POINTER(MULT),
I receive a different error,
D:\codes\Simlib-1.0\bin\lib>mult_test
3.0 2.0 6.0
Traceback (most recent call last):
File "D:\codes\Simlib-1.0\bin\lib\mult_test.py", line 18, in <module>
q = mult(4,5)
WindowsError: exception: access violation reading 0x0000000000000004
I would be very thankful for any help
someone out there may be able to offer ,
Cheers, John McElhaney
|