Python ctypes 中字段的回调函数
我正在尝试使用 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)
I am trying to register callback functions for .dll
library in Python with ctypes. But it requires callback functions in structure/field. Because it doesn't work (no errors but callback functions are doing nothing) I suppose I am wrong. Could someone, please, help me?
There is a code hopefully explaining what I am trying to do:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须初始化
tHandlerStructure
:您的代码中存在其他语法错误。最好剪切并粘贴出现错误的代码,并提供回溯。下面的作品:
You have to initialize
tHandlerStructure
:There are other syntax errors in your code. It is best to cut-and-paste the code giving you an error, and provide the tracebacks as well. Below works:
如果这是您正在使用的完整代码,那么您已经定义并实例化了该结构,但从未真正将回调放入其中。
If this is the complete code you're using, then you've defined and instantiated the structure, but never actually put your callbacks in it.