List Info

Thread: Error when trying to embed python into C++




Error when trying to embed python into C++
country flaguser name
United States
2008-05-07 15:47:07

Hi – I’m a newbie to doing this and would appreciate any help / suggestions. Here’;s my problem, I have an existing python application and a 3rd party C++ dll that need to talk to each other. So far I have been able to follow the “Extending and Embedding PythonR21; tutorial to create an extension to make the C++ dll available to my python app and have it (the extension) return back values (ints, strings, etc) to the python app.

 

The 3rd party dll has one method that I need to use that requires a callback with a particular signature. My thinking is as follows:

 

  1. create callback method in my python module
  2. create a method in my C++ extension that would take as a parameter the method from step 1 and store it
  3. create another method in my C++ extension that would act as the 3rd party dll callback with the particular signature
  4. set the method in #3 to be the callback for the 3rd party dll
  5. when the method in #3 is called back to from the 3rd party, parse the data into appropriate python objects and then call the python callback  from #2

 

Problem is when I do this, I get a python.exe error and my application crashes – so any ideas on what I am missing?? I don’t have the option of using boost because this is a legacy application and do cannot introduce another framework.

 

Again, any help is appreciated!

Thanks,

Erin

 

Here is the code:

 

Python

import wx

import camera_ext  #this is the C++ ext

 

class frmMain(wx.Frame):

    def _init_ctrls(self, prnt):

           ; #we just initialize the controls on the form here

 

    def __init__(self, parent):

        self._init_ctrls(parent)

   

   def OnInstallHooker(self, event):

        installed = camera_ext.InstallFrameHooker(self._my_frame_hooker)  # is this right? Or should it be a module method, not a method of the class?

        self.txtData.SetValue("installed hooker: " + str(installed));

       

    def OnStartGrab(self, event):

        started = camera_ext.StartFrameGrab()

        self.txtData.SetValue("started grabbing: " + str(started));

       

 

    def _my_frame_hooker(self):

        # should be called from the c++ ext

        print "I was called"  # this never gets printed out ????

        return True

 

 

 

C++ (camera_ext)

 

 

static void FrameCallBack( TProcessedDataProperty* Attributes, unsigned char *Frameptr )

{

 

         ;   //once called, we need to package up to return it to python as an actual object

         ;  

         ;   PyObject *arglist;

         ;   PyObject *result;

 

         ;   printf(220;fooR21;) //this does print out so I know the 3rdparty dll (BUFUSB is calling back)

 

         ;   // All frame data is pointed by Frameptr

         ;   // All frame related attributes is in Attributes.

         ;   arglist = Py_BuildValue("i", Attributes->CameraID);

         ;   result = PyObject_CallObject(my_python_callback,arglist);

 

         ;   Py_DECREF(arglist);

 

 

}

 

static PyObject *install_frame_hooker(PyObject *dummy, PyObject *args){

 

int retVal = 0;

         ;   PyObject *result = NULL;

             PyObject *tobj;

         ;  

 

    if (!PyArg_ParseTuple(args, "O:set_callback", &tobj))

        return NULL;

   

    // make sure second argument is a function

    if (!PyCallable_Check(tobj)) {

        PyErr_SetString(PyExc_TypeError, "Need a callable object!");

    }

         ;   Py_XINCREF(tobj); /*add a reference to the new callback instance*/

         ;   Py_XDECREF(my_python_callback); /*dispose of previous callback instance */

         ;   my_python_callback = tobj; ;       /*save reference to new callback instance */

         ;  

         ;   Py_INCREF(Py_None);

         ;   result = Py_None;

         ;   /*now this method must call the camera's SDK InstallFrameHooker to install

         ;   the C++ FrameHooker - note that 0 is raw data, 1 is bmp data */

         ;              ;

         ;   retVal = BUFUSB_InstallFrameHooker(0, FrameCallBack);

 

         ;   return result;

}

 

static PyObject *start_frame_grab(PyObject *self, PyObject *args){

         ;   //starts grabbing data

         ;   int retVal = 0;

 

         ;   //start frame grab will cause camera engine to call on the frame hooker (callback) method to return camera data

         ;   retVal = BUFUSB_StartFrameGrab(0x8888);      

 

         ;   return Py_BuildValue("i", retVal);

 

}

 

static PyMethodDef camera_ext_methods[] = {

         ;   {"StartFrameGrab", start_frame_grab, METH_VARARGS, "Start grabbing frames, specify the number of frames to grab"},

         ;   {"InstallFrameHooker", install_frame_hooker, METH_VARARGS, "Set the callback function to notify caller everytime the camera engine gets a new frame";},

         ;   {NULL, NULL}

};

 

 

void initcamera_ext(void)

{

    import_array();

    Py_InitModule("camera_ext", camera_ext_methods);

}

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )