0
C++接口: 無符號長_stdcall NAPI_JsonCommand(字符* szCommand,無符號長* pulRet,字符* PBUF,整數nLen,無符號長* pulDevCode);蟒ctypes的回調C++
回調函數的定義: 的typedef長(_stdcall fMsgCallback)(INT NMSG,空隙 pUserData,字符* PBUF,INT nLen,INT nParam);
蟒(3.5.2)碼
#coding=utf-8
__author__ = 'wjyang'
import ctypes
from ctypes import *
import json
dll = ctypes.WinDLL("napi.dll")
CMPFUNC=WINFUNCTYPE(c_long,c_int,c_void_p,c_char_p,c_int,c_int)
def py_cmp_func(nMsg,pUserData,pBuf,nLen,nParam):
print("this is callback")
return 1
addr=hex(id(CMPFUNC(py_cmp_func)))
dict={"KEY":"CONNECTREGISTER","PARAM":{"CALLBACK":addr,"AUTOPIC":0,"IP":"192.168.150.30","PORT":5556}}
dicts=json.dumps(dict)
commd=c_char_p(dicts.encode())
print(dll.NAPI_JsonCommand(commd,c_int(0),None,c_int(0),None))
,
其結果是,如果使用的話
dict={"KEY":"CONNECTREGISTER","PARAM":{"CALLBACK":CMPFUNC(py_cmp_func),"AUTOPIC":0,"IP":"192.168.150.30","PORT":5556}}
dicts=json.dumps(dict)
將是錯誤的回調函數py_cmp_func不執行
!類型錯誤:在0x021BB8A0 WinFunctionType對象不是JSON序列化
回調函數的JSON字符串,如何在回調函數的內存地址傳遞給C++?