Loading c DLL with python, problems with pointer

  c++, callback, ctypes, dll, python

Currently I have C++ loading DLL. I need replace C++ code with python. My problems are:

  1. in callback function device_ID1_callback, all values seems empty, i’m guessing i did not use pointer correctly.
  2. after call device_get_info, all values are 0, I suppose get some values none zero back.
    I have tried anything I can think of for weeks but with very little luck.

To simplified the problem, here’s partial of my code. Thanks for your time and help!!

in my lib.h file, i have

typedef unsigned int DeviceHandler;
typedef struct {
        unsigned int fpga_version;
}DeviceInfo_t;
typedef struct {
        unsigned int check_id;
        float distance[256];
}MeasureResult_t;
DLLEPXORT int EXCALL device_add(DeviceHandler* outHandler, char* device_ip, MeasureModeCallback callback);
DLLEPXORT void EXCALL device_get_info(DeviceHandler handler, DeviceInfo_t* p_device_info);

in sample C++ file:
"""

void device_ID1_callback(const void *out,unsigned int out_num){
    MeasureResult_t *ptr = (MeasureResult_t *)out;
    printf("[ChechID:0x%x] %d pack's datan",ptr[0].check_id,out_num);
}
void demo_callback_mode(){
    int ret;
    DeviceHandler device_handler;
    DeviceInfo_t device_info;

    ret = device_add(&device_handler,"192.168.1.2",&device_ID1_callback);
    device_get_info(device_handler,&device_info);

    printf("[FPGA] version : %dn", device_info.fpga_version);
}

"""
*end of c++ *

Here’s my python code:
"""

import ctypes   as c

class MeasureResult_t(c.Structure):
    _fields_  = [
            ('check_id', c.c_int),
            ('distance[256]', c.c_float)]
    
class DeviceInfo_t(c.Structure):
    _fields_  = [
            ('fpga_version', c.c_int)
            ]
    
def device_ID1_callback(out, out_num):
    print("---enter device call back function---")
    print(dir(out))
    print("out: ",out.contents)
    #print(dir(out))
    print("out_num:",out_num)
    print("---exit device call back function---nn")
    return 0

_dev = c.CDLL("./OPSensor/osp_lidar")

T_device_handler = c.c_int
T_device_handler_ptr = c.POINTER(T_device_handler)

_dev.device_add.argtypes = [T_device_handler_ptr, c.c_char_p]
_dev.device_add.restype = c.c_int


device_handler = c.c_int()
ip_val = c.c_char_p("192.168.1.2".encode('utf-8'))

    
out = MeasureResult_t()
out_num = c.c_int()


CMPFUNC_t = c.CFUNCTYPE(None, c.POINTER(MeasureResult_t), c.c_int)
MeasureModeCallback = CMPFUNC_t(device_ID1_callback)

                   
ret = _dev.device_add(c.byref(device_handler), (ip_val), MeasureModeCallback(c.byref(out), out_num))

    
_dev.device_get_info.argtypes = [T_device_handler_ptr, c.POINTER(DeviceInfo_t)]
_dev.device_get_info.restype = c.c_void_p # assume it returns C int

p_device_info = DeviceInfo_t()
_dev.device_get_info(c.byref(device_handler), c.byref(p_device_info))

print(device_handler) # I have correct device_handler value 
print(p_device_info.fpga_version) # the value i got is 0, does seem right

"""

Source: Windows Questions C++

LEAVE A COMMENT