Jul-31-2022, 10:47 PM
try to make a callback to the c++ from python code
added this c++ function
added this c++ function
extern "C" { PyObject* CPython::spam_system(PyObject* self, PyObject* args) { const char* command; int sts; if (!PyArg_ParseTuple(args, "s", &command)) return NULL; sts = system(command); return PyLong_FromLong(sts); } }trying to call back to c++ using ctypes, but not sure how to setup the args and returns when defining the c++ function in python
import ctypes class TestClass: def __init__(self, count): self.count = count self.lib = ctypes.CDLL( r'C:\Users\WINSIM\PycharmProjects\testProject\cpp\cppTester\x64\Release\cppPythonWrapper.dll') # c++ function self.spam_system = getattr(self.lib, "?spam_system@CPython@@QEAAPEAU_object@@PEAU2@0@Z") # how to setup the c++ function callback? self.spam_system.argtypes = [c_string] self.spam_system.restype = c_long # the callback function def print_cb(self): self.spam_system("ls -s") def print_hi(self, name): if __name__ == '__main__': print(f'PYTHON: function is being run directly {name} {self.count}') self.print_cb() return "Completed" else: print(f'PYTHON: function is being imported {name} {self.count}') return "Skipped"