Skip to content Skip to sidebar Skip to footer

Writing Python Ctypes For Function Pointer Callback Function In C

I am trying to write python code to call dll functions and stuck at the function below, which I believe is related to the typedef callback function or the function pointer thing.

Solution 1:

Your callback type has the wrong signature; you forgot the result type. It's also getting garbage collected when the function exits; you need to make it global.

Your GetStatus call is missing the argument pArg. Plus when working with pointers you need to define argtypes, else you'll have problems on 64-bit platforms. ctypes' default argument type is a C int.

from ctypes import * 

api = CDLL('API.dll')
StatusCB = WINFUNCTYPE(None, c_int, c_int, c_void_p)

GetStatus = api.GetStatus
GetStatus.argtypes = [StatusCB, c_void_p]
GetStatus.restype = Nonedefstatus_fn(nErrorCode, nSID, pArg):        
    print'Hello world'print pArg[0]  # 42?# reference the callback to keep it alive
_status_fn = StatusCB(status_fn)

arg = c_int(42) # passed to callback?    defstart():        
    GetStatus(_status_fn, byref(arg))

Post a Comment for "Writing Python Ctypes For Function Pointer Callback Function In C"