从具有字符缓冲区的 DLL 为 C 函数创建 python 回调。

Creating a python callback for a C function from a DLL with a char buffer.

我正在尝试为 C DLL 创建一个 python 包装器。 C DLL 通过来自 DLL 的 UDP 使用对 "send" 消息的回调。

我期望发生的是 python 代码将加载 DLL 库和两个函数。然后它将使用 RegisterCallbackSendMessage 函数向 DLL 注册回调。指向回调的指针将存储在 DLL 内存 space 的内存中。然后 python 代码将调用 SendWhoIs DLL 函数。此函数将创建一个缓冲区并使用 SendMessage 函数指针将消息从 DLL 发送到 python 代码。

我遇到的问题是 python 中 SendMessage 函数中的 message 参数不知道如何解释 void * 而我不能将此缓冲区传递给 sock.sendto 函数。

我收到以下错误。

sock.sendto(message, (UDP_IP, UDP_PORT))
TypeError: a bytes-like object is required, not 'int'

我的问题是:如何将 c_void_p 转换为 sock.sendto 可以接受的字节数组?

我已尝试尽可能地减少我的代码并使其仍然易于理解。

这是我的 C 代码

// C DLL code 
// 

#define DllExport extern "C" __declspec( dllexport )

typedef uint16_t(*FPCallbackSendMessage)(const uint8_t * message, const uint32_t length);
FPCallbackSendMessage g_CallbackSendMessage;

DllExport bool RegisterCallbackSendMessage(uint16_t(*p_CallbackSendMessage)(const uint8_t * message, const uint32_t length)) {
    g_CallbackSendMessage = p_CallbackSendMessage ; 
    return true;
}

void SendWhoIs(unsigned int dd) {
    printf("dd=%d", dd); 
    char buffer[500];
    g_CallbackSendMessage( buffer, 500 ); 
}

这是我的 python 代码

# Python code 
# =================
print("Start") 

customDLL = cdll.LoadLibrary ("customeDLL.dll")

def SendMessage( message, length ):
    print("SendMessage...") 
    UDP_IP = "192.168.1.1"
    UDP_PORT = 47808
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(message, (UDP_IP, UDP_PORT))
    return True 

print("Registering Callback SendMessage...") 
SendMessageFUNC = CFUNCTYPE(c_bool, c_void_p, c_uint)
SendMessage_func = SendMessageFUNC( SendMessage )

RegisterCallbackSendMessage = customDLL.RegisterCallbackSendMessage
RegisterCallbackSendMessage.argtypes = [ SendMessageFUNC ]
RegisterCallbackSendMessage( SendMessage_func ) 

print("Do message...") 
SendWhoIs = BACnetStackDLL.SendWhoIs
SendWhoIs.argtypes = [c_uint]

SendWhoIs( 47 )

print("End") 

我明白了。

在 python SendMessage 函数中,我找到了一种将 c_void_p 转换为字节数组的方法。

# convert the pointer to a buffer. 
buffer = c_char * length 
messageToSend = buffer.from_address(message)