samedi 19 janvier 2019

using python ctypes pass a long vector by reference to a c++ function

Am trying to pass an empty vector of type long by reference to a c++ function as an argument from python using ctypes.

Im using python3 and c++11 compiler. I've tried passing a ctypes.c_void_p pointer but it gives the wrong result.

test.cpp

extern "C" {
 int pke_decrypt(char *a,char *b,vector<long> &v)
  {do something;
   assign v;
   }}

test.py

_p=ctypes.CDLL('/home/electrical/Downloads/src/libpke.so')
 _p.pke_decrypt.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(ctypes.c_void_p))]
 def decrypt(input1,input2):
    global _p   
    v=ctypes.c_void_p()
    print (v)
    result=_p.pke_decrypt(input1.encode('utf-8'),input2.encode('utf-8'),ctypes.byref(v))
    print (v)
    return

I'am expecting to get a random sized array(which is computed in the c++ function) of type long as output. That is, the size of the resultant vector is not predefined, else i could have used numpy.ndpointers() and defined a return type and returned a dynamically allocated array from c.

Aucun commentaire:

Enregistrer un commentaire