我试图用ctypes为Python中的.dll
库注册回调函数。但它需要结构/字段中的回调函数。因为它不起作用(没有错误,但是回调函数什么也没有做),我想我错了。有人能帮帮我吗?在
有一个代码希望能解释我要做的事情:
import ctypes
firsttype = CFUNCTYPE(c_void_p, c_int)
secondtype = CFUNCTYPE(c_void_p, c_int)
@firsttype
def OnFirst(i):
print "OnFirst"
@secondtype
def OnSecond(i):
print "OnSecond"
class tHandlerStructure(Structure):
`_fields_` = [
("firstCallback",firsttype),
("secondCallback",secondtype)
]
stHandlerStructure = tHandlerStructure()
ctypes.cdll.myDll.Initialize.argtypes = [POINTER(tHandlerStructure)]
ctypes.cdll.myDll.Initialize.restype = c_void_p
ctypes.cdll.myDll.Initialize(stHandleStructure)
您必须初始化
tHandlerStructure
:您的代码中还有其他语法错误。最好剪切并粘贴给您错误的代码,并提供回溯。以下工程:
^{pr2}$如果这是您正在使用的完整代码,那么您已经定义并实例化了该结构,但从未实际将回调放入其中。在
相关问题 更多 >
编程相关推荐