I have a simple working program that sends an int ( num_piezas in the example) from a Python function to a C++ function.
foo.cpp
#include <iostream> class Foo{ public: void bar(int number){ printf("Number is: %d\n", number); } }; extern "C" { Foo* Foo_new(){ return new Foo(); } void Foo_bar(Foo* foo, int number){ foo->bar(number); } }
fooWrapper.py
from ctypes import cdll lib = cdll.LoadLibrary('./libfoo.so') class Foo(object): def __init__(self): self.obj = lib.Foo_new() def bar(self, num_piezas): lib.Foo_bar(self.obj, num_piezas) num_piezas = 5 f = Foo() f.bar(num_piezas)
The thing is, now I want to send an int array and a char array with this number. So I tried:
foo.cpp
#include <iostream> #include <string.h> class Foo{ public: void bar(int number, int piezas[3], char prioridades[3][20]){ printf("Number is: %d\n", number); for(int i=0;i<3;i++) { printf("%d", piezas[i]); } for(int q=0;q<3;q++) { printf("%s ", prioridades[q]); } } }; extern "C" { Foo* Foo_new(){ return new Foo(); } void Foo_bar(Foo* foo, int number, int piezas[3], char prioridades[3][20]){ foo->bar(number, piezas, prioridades); } }
fooWrapper.py
from ctypes import cdll lib = cdll.LoadLibrary('./libfoo.so') class Foo(object): def __init__(self): self.obj = lib.Foo_new() def bar(self, num_piezas, piezas, prioridades): lib.Foo_bar(self.obj, num_piezas, piezas, prioridades) piezas = [1, 2, 3] prioridades = ['normal', 'baja', 'primera pieza'] num_piezas = 5 f = Foo() f.bar(num_piezas, piezas, prioridades)
The C++ file compiles correctly, but when I try to execute the Python function ( python fooWrapper.py ), this error message appears:
Traceback (most recent call last): File "fooWrapper.py", line 15, in <module> f.bar(num_piezas, piezas, prioridades) File "fooWrapper.py", line 9, in bar lib.Foo_bar(self.obj, num_piezas, piezas, prioridades) ctypes.ArgumentError: argument 3: <type 'exceptions.TypeError'>: Don't know how to convert parameter 3
What am I doing wrong? Do I need to do something to pass int and string arrays as parameters? Thanks in advance.