ctypes - Passing an array from python to shared DLL, seeing access violation error -
the c function dll:
multi(double freq, double power, int ports[], int size) need pass 3rd parameter array python
tried following different codes:
a:
import ctypes pyarr = [2,3] arr = (ctypes.c_int * len(pyarr))(*pyarr) lp = cdll(cdll_file) lp.multi(c_double(freq), c_double(power), arr ,c_int(size))`
this code shows error ## exception :: access violation reading 0x00000000000
b:
retarr = (ctypes.c_int*2)() retarr[0] =2 retarr[1] =3 lp = cdll(cdll_file) lp.multi(c_double(freq), c_double(power), retarr ,c_int(size))`
this code shows error
## exception :: access violation reading 0x00000000000
c: same code using ctypes.byref tried ......
my understanding function expects array argument , tries passing array such address. both cases , didn't work sees mistake in understanding or other work out ??
specify argtypes
. given test.dll source:
#include <stdio.h> __declspec(dllexport) void multi(double freq, double power, int ports[], int size) { int i; printf("%f %f\n",freq,power); for(i = 0; < size; ++i) printf("%d\n",ports[i]); }
this works:
from ctypes import * dll = cdll('test') multi = dll.multi multi.argtypes = (c_double,c_double,pointer(c_int),c_int) multi.restype = none ports = (c_int * 2)(100,200) multi(1.1,2.2,ports,len(ports))
output:
1.100000 2.200000 100 200
Comments
Post a Comment