python - c pointers and ctypes -
so, have c++ class wrap in c can use in python using ctypes. declaration of c++ class:
// test.h class test { public: static double add(double a, double b); }; //test.cpp #include "stdafx.h" #include "test.h" double test::add(double a, double b) { return + b; }
c wrap:
// cdll.h #ifndef wrapdll_exports #define wrapdll_api __declspec(dllexport) #else #define wrapdll_api __declspec(dllimport) #endif #include "test.h" extern "c" { wrapdll_api struct testc; wrapdll_api testc* newtest(); wrapdll_api double addc(testc* pc, double a, double b); } //cdll.cpp #include "stdafx.h" #include "cdll.h" testc* newtest() { return (testc*) new test; } double addc(testc* pc, double a, double b) { return ((test*)pc)->add(a, b); }
python script:
import ctypes t = ctypes.cdll('../debug/cdll.dll') = t.newtest() t.addc(a, 2, 3)
result of t.addc(a, 2, 3) negative integer. there problem pointer, not know problem. have ideas?
as addc
static function pointer not problem.
you need pass double
values addc
, , double type back:
t.addc.restype = c_double t.addc(a, c_double(2), c_double(3))
the documentation ctype explains this.
Comments
Post a Comment