Python ctypes 中字段的回调函数

发布于 2024-10-12 04:55:46 字数 701 浏览 6 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分分钟 2024-10-19 04:55:46

您必须初始化tHandlerStructure

stHandlerStructure = tHandlerStructure(OnFirst,OnSecond)

您的代码中存在其他语法错误。最好剪切并粘贴出现错误的代码,并提供回溯。下面的作品:

from ctypes import *

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(OnFirst,OnSecond)

cdll.myDll.Initialize.argtypes = [POINTER(tHandlerStructure)]
cdll.myDll.Initialize.restype = c_void_p

cdll.myDll.Initialize(stHandlerStructure)

You have to initialize tHandlerStructure:

stHandlerStructure = tHandlerStructure(OnFirst,OnSecond)

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:

from ctypes import *

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(OnFirst,OnSecond)

cdll.myDll.Initialize.argtypes = [POINTER(tHandlerStructure)]
cdll.myDll.Initialize.restype = c_void_p

cdll.myDll.Initialize(stHandlerStructure)
攒眉千度 2024-10-19 04:55:46

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

stHandlerStructure = tHandlerStructure(OnFirst, OnSecond)

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.

stHandlerStructure = tHandlerStructure(OnFirst, OnSecond)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文