2
votes

I integrate a dll to python by ctypes, the dll need set a callback function. But the dll callback function declare as below

void setCallBack(void(*)(Data &a))

Data is C struct, declare as below
typedef struct {
    int index,
    ....
}Data

I do not clear how to set it by ctypes, I try it as below, but return "WindowsError: [Error -1073741795] Windows Error 0xC000001D" error, could you help to give a example how to set the callback.

mydll = cdll.LoadLibrary("XXX.dll")  
callback_type = CFUNCTYPE(None, POINTER(Data))
callback = callback_type(Data)
mydll.setCallBack(callback)

def dataResponse(data):
    print data.index

class Data(ctypes.Structure):
    _fields_ = [("index", c_int)]
3

3 Answers

1
votes

Your structure looks correct, but if you're calling into a Windows API your calling convention is likely off (you're using cdecl, but if this is a Windows API call, you need stdcall).

Try replacing mydll = cdll.LoadLibrary("XXX.dll") with mydll = WinDLL("XXX.dll")

You may also need to provide a callback function that uses stdcall instead of cdecl. If so, switch CFUNCTYPE to WINFUNCTYPE. The API you're calling should provide details about what it expects.

And see if that helps.

1
votes

One error is that your callback is wrapping your data structure, not your callback function. Change:

callback_type = CFUNCTYPE(None, POINTER(Data))
callback = callback_type(Data)
mydll.setCallBack(callback)
def dataResponse(data):
    print data.index

to:

def dataResponse(data):
    print data.index
callback = callback_type(dataResponse)
mydll.setCallBack(callback)

Or you can use decorator syntax:

@CFUNCTYPE(None, POINTER(Data))
def dataResponse(data):
    print data.index

mydll.setCallBack(dataResponse)

Here's a complete example. Also use extern "C" to avoid C++ name mangling:

test.cpp (Windows)

struct Data {
    int index;
};

typedef void (*CALLBACK)(Data&);
CALLBACK g_callback;

extern "C" {
    __declspec(dllexport)void setCallBack(CALLBACK f) {
        g_callback = f;
    }

    __declspec(dllexport) void func() {
        Data d;
        d.index = 42;
        if(g_callback)
            g_callback(d);
    }
}

test.py

from ctypes import *

class Data(Structure):
    _fields_ = [('index',c_int)]

CALLBACK = CFUNCTYPE(None,POINTER(Data))

dll = CDLL('test')
dll.setCallBack.argtypes = CALLBACK,
dll.setCallBack.restype = None
dll.func.argtypes = None
dll.restype = None

@CALLBACK
def dataResponse(data):
    print(data.contents.index)

dll.setCallBack(dataResponse)
dll.func()

Output

42
0
votes

ctypes does not work with Cpp it only works with C, your Cpp function need to have extern "C" linkage.
The reason that does not work has to do with Cpp name mangling. The name of your function void setCallBack(void(*)(Data &a)) is mangled in Cpp as _Z11setCallBackPFvR4DataE, but if you put extern "C" in from of it (extern "C" void setCallBack(void(*)(Data &a))), it will C mangling rules, so it will be mangled as setCallBack.
So when you do mydll.setCallBack ctypes is looking for a function named setCallBack but it doesn't find it, it finds a function named _Z11setCallBackPFvR4DataE which it does not recognize and gives you an error.