如何转换回叫结果? [英] How to convert a callback result?
本文介绍了如何转换回叫结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是ctypes的新手,但我想使用以下回调签名创建一个回调函数:
def autosetup_callback(p0 ,p1,p2):
这是我的回调函数签名
void(__ stdcall * pGUIFunction)(SIndex sIndex,unsigned int statusFlag,unsigned int,const char message [512])
print('autosetup arguments',p0,p1,p2)
sn = c.c_uint(0)
autosetup_cb_format = c.CFUNCTYPE (sn,c.c_uint,c.c_uint,c.c_char)
调用此回调时,我收到以下错误:
文件 _ctypes / callbacks.c,转换回调结果 $ b中的第257行
$ b TypeError:需要一个整数(got类型NoneType)
设置:值p1,p2,p3:0 0 0
自动设置参数0 0 b'\x18 '###这是回调应打印的内容
在以下情况中忽略的异常:< function autosetup_callback at 0x000001E3D4135A60>
有什么想法吗?
解决方案
您的示例中有一些不一致之处:
- 该函数的原型有四个参数,但是只有三个您的Python实现。
-
__ stdcall
应该使用WINFUNCTYPE
而不是CFUNCTYPE
。 -
sn
是实例,而不是类型。回调定义的第一个参数是返回值(Python中的void
,None
)。 - 最后一个参数类型为
char [512]
(衰减为char *
,因此<$ c
这是一个可行的示例。给出: p>
test.c
#define API __declspec(dllexport)// Windows特定的导出
typedef int SIndex;
typedef void(__ stdcall * CALLBACK)(SIndex sIndex,unsigned int statusFlag,unsigned int,const char消息[512] );
CALLBACK g_callback;
API void set_callback(CALLBACK pFunc)
{
g_callback = pFunc;
}
API void call()
{
g_callback(1,2,3, Hello);
}
test.py
来自ctypes import *
CALLBACK = WINFUNCTYPE(None,c_int,c_uint,c_uint,c_char_p)
@CALLBACK
def autosetup_callback(p0,p1,p2,p3):
print('autosetup arguments',p0,p1,p2,p3)
dll = CDLL('test')
dll.set_callback.argtypes =回调,
dll.set_callback.restype =无
dll.call.argtypes =无
dll.call.restype =无
dll.set_callback(autosetup_callback)
dll.call()
输出:
自动设置参数1 2 3 b'Hello'
I am new to ctypes but I want to create a callback function with the following callback signature:
def autosetup_callback(p0, p1, p2):
"""This is my callback function signature
void(__stdcall *pGUIFunction)(SIndex sIndex, unsigned int statusFlag, unsigned int, const char message[512])
"""
print('autosetup arguments', p0, p1, p2)
sn= c.c_uint(0)
autosetup_cb_format = c.CFUNCTYPE(sn, c.c_uint, c.c_uint, c.c_char)
When this callback is called, I get the following errors:
File "_ctypes/callbacks.c", line 257, in 'converting callback result'
TypeError: an integer is required (got type NoneType)
settings: values p1,p2,p3: 0 0 0
autosetup arguments 0 0 b'\x18' ### This is what the callback should print
Exception ignored in: <function autosetup_callback at 0x000001E3D4135A60>
Any ideas?
解决方案
There's some inconsistency in your example:
- The prototype of your function takes four arguments but you only have three in your Python implementation.
__stdcall
should useWINFUNCTYPE
notCFUNCTYPE
.sn
is an instance, not a type. The first parameter of the callback definition is the return value (void
,None
in Python).- The last parameter type is
char[512]
(decays tochar*
soc_char_p
is needed in the callback definition.
Here's a working example. Given:
test.c
#define API __declspec(dllexport) // Windows-specific export
typedef int SIndex;
typedef void(__stdcall *CALLBACK)(SIndex sIndex, unsigned int statusFlag, unsigned int, const char message[512]);
CALLBACK g_callback;
API void set_callback(CALLBACK pFunc)
{
g_callback = pFunc;
}
API void call()
{
g_callback(1,2,3,"Hello");
}
test.py
from ctypes import *
CALLBACK = WINFUNCTYPE(None,c_int,c_uint,c_uint,c_char_p)
@CALLBACK
def autosetup_callback(p0, p1, p2, p3):
print('autosetup arguments', p0, p1, p2, p3)
dll = CDLL('test')
dll.set_callback.argtypes = CALLBACK,
dll.set_callback.restype = None
dll.call.argtypes = None
dll.call.restype = None
dll.set_callback(autosetup_callback)
dll.call()
Output:
autosetup arguments 1 2 3 b'Hello'
这篇关于如何转换回叫结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文