C++インタフェース: unsigned long型_stdcall NAPI_JsonCommand(CHAR * szCommand、符号なしのロング* pulRet、CHAR * PBUF、nLenをINT、符号なしのロング* pulDevCode)。PythonのctypesのコールバックC++
コールバック関数の定義: typedefは長い(_stdcall fMsgCallback)(int型NMSG、無効 pUserData、CHAR * 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が
を実行しないことです! TypeError:0x021BB8A0のWinFunctionTypeオブジェクトがJSONのシリアライズ可能ではありません
json文字列内のコールバック関数は、どのようにコールバック関数のメモリアドレスがC++に渡されましたか?