Python ctypes中字段中的回调函数

2024-10-02 10:20:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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)

Tags: 函数defctypesintprintvoidinitializecdll
2条回答

您必须初始化tHandlerStructure

stHandlerStructure = tHandlerStructure(OnFirst,OnSecond)

您的代码中还有其他语法错误。最好剪切并粘贴给您错误的代码,并提供回溯。以下工程:

^{pr2}$

如果这是您正在使用的完整代码,那么您已经定义并实例化了该结构,但从未实际将回调放入其中。在

stHandlerStructure = tHandlerStructure(OnFirst, OnSecond)

相关问题 更多 >

    热门问题