在 python 中使用 ctypes 调用的函数的正确回调签名是什么?

发布于 2024-12-12 03:55:31 字数 765 浏览 0 评论 0原文

我必须在 Python 中定义一个回调函数,该函数将从 DLL 中调用。

BOOL setCallback (LONG nPort, void ( _stdcall *pFileRefDone) (DWORD nPort, DWORD nUser), DWORD nUser);

我尝试了这段代码,它似乎在 Python 2.5 中工作,但在 Python 2.7 中它崩溃了,我认为我做错了什么。

import ctypes, ctypes.wintypes
from ctypes.wintypes import DWORD

def cbFunc(port, user_data):
        print "Hurrah!"

CB_Func = ctypes.WINFUNCTYPE(None, DWORD, DWORD)

mydll.setCallback(ctypes.c_long(0), CB_Func(cbFunc), DWORD(0))

错误在哪里?

注意:该平台仅在 32 位上运行(Windows 和 Python)。 DLL 加载成功,并且从 Python 调用时,内部的其他函数也可以正常工作。

重现此内容的完整示例代码可在 https://github.com/ssbarnea/pyHikvision 获取 - 只需在 py25 和 py27 下运行 Video.py。

I have to define a callback function in Python, one that will be called from a DLL.

BOOL setCallback (LONG nPort, void ( _stdcall *pFileRefDone) (DWORD nPort, DWORD nUser), DWORD nUser);

I tried this code, that seems to work in Python 2.5 but with Python 2.7 it crashes and I assume I did something wrong.

import ctypes, ctypes.wintypes
from ctypes.wintypes import DWORD

def cbFunc(port, user_data):
        print "Hurrah!"

CB_Func = ctypes.WINFUNCTYPE(None, DWORD, DWORD)

mydll.setCallback(ctypes.c_long(0), CB_Func(cbFunc), DWORD(0))

Where is the mistake?

Note: The platform is running on 32bit only (both Windows and Python). The DLL loads successfully and also other functions from inside do work just fine while called from Python.

A full sample code that reproduces this is available at https://github.com/ssbarnea/pyHikvision - just run Video.py under py25 and py27.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

花开柳相依 2024-12-19 03:55:31

您的 CB_Func(cbFunc) 参数在 setCallback 函数之后立即被垃圾收集。只要可以调用回调,该对象就必须持续存在(15.17 .1.17. 回调函数,最后一段)。将其分配给一个变量并保留它。这是我的工作示例:

DLL

typedef unsigned int DWORD;
typedef long LONG;
typedef int BOOL;
#define TRUE  1
#define FALSE 0

typedef void (__stdcall *CALLBACK)(DWORD,DWORD);

CALLBACK g_callback = 0;
DWORD g_port = 0;
DWORD g_user = 0;

BOOL __declspec(dllexport) setCallback (LONG nPort, CALLBACK callback, DWORD nUser)
{
    g_callback = callback;
    g_port = nPort;
    g_user = nUser;
    return TRUE;
}

void __declspec(dllexport) Fire()
{
    if(g_callback)
        g_callback(g_port,g_user);
}

Failing Script

from ctypes import *

def cb_func(port,user):
    print port,user

x = CDLL('x')
CALLBACK = WINFUNCTYPE(None,c_uint,c_uint)
#cbfunc = CALLBACK(cb_func)
x.setCallback(1,CALLBACK(cb_func),2)
x.Fire()

Passing Script

from ctypes import *

def cb_func(port,user):
    print port,user

x = CDLL('x')
CALLBACK = WINFUNCTYPE(None,c_uint,c_uint)
cbfunc = CALLBACK(cb_func)
x.setCallback(1,cbfunc,2)
x.Fire()

Edit:此外,由于 CALLBACK 是一个返回函数的函数,因此它可以用作 Python 回调的装饰器,从而消除回调超出范围的问题:

from ctypes import * 

CALLBACK = WINFUNCTYPE(None,c_uint,c_uint) 

@CALLBACK
def cb_func(port,user): 
    print port,user 

x = CDLL('x') 
x.setCallback(1,cb_func,2) 
x.Fire() 

Your CB_Func(cbFunc) parameter gets garbage-collected right after the setCallback function. That object has to persist as long as the callback can be called (15.17.1.17. Callback functions, last paragraph). Assign it to a variable and keep it around. Here's my working example:

DLL

typedef unsigned int DWORD;
typedef long LONG;
typedef int BOOL;
#define TRUE  1
#define FALSE 0

typedef void (__stdcall *CALLBACK)(DWORD,DWORD);

CALLBACK g_callback = 0;
DWORD g_port = 0;
DWORD g_user = 0;

BOOL __declspec(dllexport) setCallback (LONG nPort, CALLBACK callback, DWORD nUser)
{
    g_callback = callback;
    g_port = nPort;
    g_user = nUser;
    return TRUE;
}

void __declspec(dllexport) Fire()
{
    if(g_callback)
        g_callback(g_port,g_user);
}

Failing Script

from ctypes import *

def cb_func(port,user):
    print port,user

x = CDLL('x')
CALLBACK = WINFUNCTYPE(None,c_uint,c_uint)
#cbfunc = CALLBACK(cb_func)
x.setCallback(1,CALLBACK(cb_func),2)
x.Fire()

Passing Script

from ctypes import *

def cb_func(port,user):
    print port,user

x = CDLL('x')
CALLBACK = WINFUNCTYPE(None,c_uint,c_uint)
cbfunc = CALLBACK(cb_func)
x.setCallback(1,cbfunc,2)
x.Fire()

Edit: Also, since CALLBACK is a function returning a function, it can be used as a decorator for the Python callback, eliminating problem of the callback going out of scope:

from ctypes import * 

CALLBACK = WINFUNCTYPE(None,c_uint,c_uint) 

@CALLBACK
def cb_func(port,user): 
    print port,user 

x = CDLL('x') 
x.setCallback(1,cb_func,2) 
x.Fire() 
合久必婚 2024-12-19 03:55:31

函数的 Video 类 的上下文中:

class Video(object):

    def __init__(self, ...):
        self.__cbFileRefDone = []

    def open(self, filename):
        @WINFUNCTYPE(None, DWORD, DWORD)
        def cbFileRefDone(port, user_data):
            print "File indexed.", filename
        self.__cbFileRefDone.append(cbFileRefDone) # save reference

        if not self.hsdk.PlayM4_SetFileRefCallBack(
            c_long(self.port), cbFileRefDone, DWORD(0)):
            logging.error("Unable to set callback for indexing")
            return False

在使用嵌套 有点难看,但更自然的变体在回调期间失败并出现 TypeError:

#XXX this won't work
@WINFUNCTYPE(None, DWORD, DWORD)
def cbFileRefDone(self, port, user_data):
    print "File indexed."

要修复可以创建特殊函数描述符的问题:

def method(prototype):
    class MethodDescriptor(object):
        def __init__(self, func):
            self.func = func
            self.bound_funcs = {} # hold on to references to prevent gc
        def __get__(self, obj, type=None):
            assert obj is not None # always require an instance
            try: return self.bound_funcs[obj,type]
            except KeyError:
                ret = self.bound_funcs[obj,type] = prototype(
                    self.func.__get__(obj, type))
                return ret
    return MethodDescriptor

用法:

class Video(object):

    @method(WINFUNCTYPE(None, DWORD, DWORD))
    def cbFileRefDone(self, port, user_data):
        print "File indexed."

    def open(self, filename):
        # ...
        self.hsdk.PlayM4_SetFileRefCallBack(
            c_long(self.port), self.cbFileRefDone, DWORD(0))

另外 MethodDescriptor 保存对 C 函数的引用以防止它们被垃圾收集。

In the context of Video class using a nested function:

class Video(object):

    def __init__(self, ...):
        self.__cbFileRefDone = []

    def open(self, filename):
        @WINFUNCTYPE(None, DWORD, DWORD)
        def cbFileRefDone(port, user_data):
            print "File indexed.", filename
        self.__cbFileRefDone.append(cbFileRefDone) # save reference

        if not self.hsdk.PlayM4_SetFileRefCallBack(
            c_long(self.port), cbFileRefDone, DWORD(0)):
            logging.error("Unable to set callback for indexing")
            return False

It is somewhat ugly but a more natural variant fails with TypeError during callback:

#XXX this won't work
@WINFUNCTYPE(None, DWORD, DWORD)
def cbFileRefDone(self, port, user_data):
    print "File indexed."

To fix that a special function descriptor could be created:

def method(prototype):
    class MethodDescriptor(object):
        def __init__(self, func):
            self.func = func
            self.bound_funcs = {} # hold on to references to prevent gc
        def __get__(self, obj, type=None):
            assert obj is not None # always require an instance
            try: return self.bound_funcs[obj,type]
            except KeyError:
                ret = self.bound_funcs[obj,type] = prototype(
                    self.func.__get__(obj, type))
                return ret
    return MethodDescriptor

Usage:

class Video(object):

    @method(WINFUNCTYPE(None, DWORD, DWORD))
    def cbFileRefDone(self, port, user_data):
        print "File indexed."

    def open(self, filename):
        # ...
        self.hsdk.PlayM4_SetFileRefCallBack(
            c_long(self.port), self.cbFileRefDone, DWORD(0))

Additionally MethodDescriptor saves references to C functions to prevent them being garbage collected.

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