[C++-sig] Problems with Implementing Callback Python/C++-DLL

Oleg Novychny oleg_python at yahoo.de
Fri Jul 29 16:11:53 CEST 2005

Dear friends,

I’m hoping to receive some tips to my problem. At the moment I’m trying to implement a callback between Python and my C++-DLL.

My DLL calculates permanently some values and stores them in member-variables in one of the DLL-classes. 

There are some exported functions in my DLL for starting and ending the evaluating and for reading out of values from the member-variables of my DLL.

My wish is to implement my DLL in such a way that my Python-script witch I use for starting my DLL will receive a notice from my DLL to that time when the new values were calculated. I can call then my Python-Function for reading out the values from the DLL synchronized.


This is my DLL-header:


SOUNDPROCLIB_API int StartProcessing(int fSourceRec, int fNoiseRec, int fCrackRec, int fAudioOnRec, int fBeepRec, unsigned long ulTimeMS );


SOUNDPROCLIB_API int GetResults(unsigned long* ulHz, double* dNoise, int* bAudio, int* bCrackFlag, double* dSNR, int* bBeep);


SOUNDPROCLIB_API int EndProcessing(void);


So I want to call my GetResults function from Python only if my DLL “tells” to my Python-Script “do it”.

Here is my Python-DLL-wrapper:


from ctypes import*

import sys



class SoundProcLib(object):

    def __init__(self, library):

        self.library = library


        @cdecl(c_int, library, [c_int, c_int, c_int, c_int, c_int, c_ulong])

        def StartProcessing(f1, f2, f3, f4, f5, ul1):           

            if 0 == StartProcessing._api_(f1, f2, f3, f4, f5, ul1):

                raise WinError()

            return None

        self.StartProcessing = StartProcessing


        @cdecl(c_int, library, [POINTER(c_ulong), POINTER(c_double), POINTER(c_int), POINTER(c_int), POINTER(c_double), POINTER(c_int)])

        def GetResults():

            ulHz,dNoise,bAudio,bCrackFlag,dSNR,bBeep = c_ulong(), c_double(), c_int(), c_int(), c_double(), c_int()

            if 0 == GetResults._api_(byref(ulHz),byref(dNoise),byref(bAudio),byref(bCrackFlag),byref(dSNR),byref(bBeep)):

                raise WinError()

            return ulHz.value,dNoise.value,bAudio.value,bCrackFlag.value,dSNR.value,bBeep.value

        self.GetResults = GetResults


        @cdecl(c_int, library, [c_void_p])

        def EndProcessing():

            if 0 == EndProcessing._api_(None):

                raise WinError()

            return None

        self.EndProcessing = EndProcessing


        @cdecl(c_void_p, library, [c_void_p])

        def inittestcallback():

            if 0 == inittestcallback._api_(None):

                raise WinError()

            return None

        self.inittestcallback = inittestcallback


At the moment I started to implement my callback in such a way (I found it in a python-tutorial):


PyObject *ppyobjArgList;

PyObject *ppyobjResult;


static PyObject *my_callback = NULL;

static PyObject *my_set_callback(PyObject *dummy, PyObject *args)


    PyObject *result = NULL;

    PyObject *temp;


    if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {

        if (!PyCallable_Check(temp)) {

            PyErr_SetString(PyExc_TypeError, "parameter must be callable");

            return NULL;


        Py_XINCREF(temp);         /* Add a reference to new callback */

        Py_XDECREF(my_callback);  /* Dispose of previous callback */

        my_callback = temp;       /* Remember new callback */

        /* Boilerplate to return "None" */


        result = Py_None;


        bPyCallBackFlag = true;


    return result;



static PyMethodDef SoundMethods[] = {

    {"callback",  my_set_callback, METH_VARARGS,

     "Execute a callback."},


    {NULL, NULL, 0, NULL}        /* Sentinel */




So I’m not sure how can I “register” my_set_callback function in the already started Python-Interpreter and to “explain” my DLL which Python-Function is to call when the new values in my DLL were calculated.


Thank you very much for your suggestions


Gesendet von Yahoo! Mail - Jetzt mit 1GB kostenlosem Speicher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050729/bae427f2/attachment.htm>

More information about the Cplusplus-sig mailing list