Category : ctypes

Background I have some functions which are written in C++, which require high real-time performance. I want to quickly export these functions as dynamic link library to be exposed to Python so that I could do some high level programming. In these functions, in order to simply usage, I use PyList_New in <Python.h> to collect ..

Read more

I have a C++ interface that contains the following: extern "C" { char const* getValForEntry(Entry e) { std::string no_val = "__err::no_val"; if (handles.find(e) != handles.end()) { std::string val = handles[e]->get_value(); return val.c_str(); } else return no_entry.c_str(); } } I have a python app that connects to this interface with ctypes like so: self.lib.getUpdateForEntry.restype = c_char_p ..

Read more

I’m trying to call C++ functions using the ctypes library in Python: test.py from ctypes import * from random import randint tester = cdll.LoadLibrary(‘./test.dll’) print(tester.test(randint(1, 100))) test.cpp #include <vector> int cppTest(int num) { std::vector<int> v; v.push_back(num); return v.back(); } extern "C" __declspec(dllexport) int test(int num) { return cppTest(num); } Then exuecute the following commands: > ..

Read more

The (abstracted) DLL includes the following: typedef struct Foo { int a; double b; } Foo; Foo* WINAPI costum_function(Foo *foo); The costum_function assigns some specific values to a, b and returns the edited instance. And my python script to use the dll is: import ctypes class Foo(ctypes.Structure): __fields_ = [("a", ctypes.c_int), ("b", ctypes.c_double)] dll = ..

Read more

I have the following files: test.h: extern "C" { void* createTest(); void getStrings(void* test_ptr, char*** strings, size_t* length); } test.cpp: #include <vector> #include "test.h" class Test { public: Test() { strings.push_back("test1"); strings.push_back("test2"); strings.push_back("test3"); strings.push_back("test4"); } std::vector<char *>& getStrings() { return strings; } private: std::vector<char *> strings; }; void* createTest() { return reinterpret_cast<void *>(new Test()); } ..

Read more

I can compile the C++ code with g++ -c -fPIC arrayAdder.cpp -o adder.o g++ -shared -Wl,-soname,libArrayAdder.so -o libArrayAdder.so adder.o I can’t seem to define it right in python. There is also an issue with the fact I’m trying to return an array, but I haven’t seen examples of it online. Below is my c++ code: ..

Read more