Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embedded Python in C++
#15
try to make a callback to the c++ from python code

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"
Reply


Messages In This Thread
Embedded Python in C++ - by Xeno - Jul-30-2022, 05:12 AM
RE: Embedded Python in C++ - by Gribouillis - Jul-30-2022, 07:25 AM
RE: Embedded Python in C++ - by Xeno - Jul-30-2022, 02:29 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-30-2022, 04:01 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 12:17 AM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 07:26 AM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 08:15 AM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 07:13 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 08:56 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 09:21 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 09:27 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 09:33 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 09:49 PM
RE: Embedded Python in C++ - by Gribouillis - Jul-31-2022, 09:51 PM
RE: Embedded Python in C++ - by Xeno - Jul-31-2022, 10:47 PM
RE: Embedded Python in C++ - by Gribouillis - Aug-01-2022, 07:35 AM
RE: Embedded Python in C++ - by Xeno - Aug-02-2022, 04:31 AM
RE: Embedded Python in C++ - by Gribouillis - Aug-02-2022, 08:08 AM
RE: Embedded Python in C++ - by Xeno - Aug-03-2022, 01:55 AM
RE: Embedded Python in C++ - by Gribouillis - Aug-03-2022, 07:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run detectron2, as python embedded code in C++, on GPU? hassaniqbal931 3 2,675 Nov-02-2023, 04:45 PM
Last Post: blabling2
  Adding libraries to embedded Python as a ZIP The_Oman 0 2,290 May-05-2023, 04:05 PM
Last Post: The_Oman
Bug Embedded Python Memory Leaks Alexei 1 1,873 Sep-16-2022, 01:15 PM
Last Post: Alexei
  Embedded python fails to compile on Raspberry Pi tryfon 2 4,853 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Can Embedded Python run any shared library on Android ? sprotz 0 3,251 Nov-08-2020, 12:21 PM
Last Post: sprotz
  memory leak on embedded python in c++ asdf3721 3 4,697 Jul-16-2020, 06:33 AM
Last Post: Gribouillis
  Embedded Python PyObject_CallObject function JRHeisey 1 3,411 Nov-27-2019, 01:50 AM
Last Post: casevh
  Trying to implement Python into embedded OS thesurya7 2 3,298 Apr-02-2019, 06:38 PM
Last Post: ebolisa
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 3,463 Mar-21-2019, 08:54 PM
Last Post: mmoelle1
  python code (embedded in Redshift) to return result of the query Mel 0 2,946 Aug-24-2018, 06:12 PM
Last Post: Mel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020