SWIG 的 Python ctypes 回调函数

发布于 2024-08-17 15:30:24 字数 203 浏览 1 评论 0原文

我有一个 SWIG C++ 函数,它需要一个函数指针 (WNDPROC),并且想给它一个由 ctypes.WINFUNCTYPE 包装的 Python 函数。

在我看来,这应该是兼容的,但 SWIG 的类型检查会引发异常,因为它不知道 ctypes.WINFUNCTYPE 类型实际上是 WNDPROC。

我该怎么做才能将我的回调传递给 SWIG 以便它理解?

I have a SWIG C++ function that expects a function pointer (WNDPROC), and want to give it a Python function that has been wrapped by ctypes.WINFUNCTYPE.

It seems to me that this should be compatible, but SWIG's type checking throws an exception because it doesn't know that the ctypes.WINFUNCTYPE type is acctually a WNDPROC.

What can I do to pass my callback to SWIG so that it understands it?

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

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

发布评论

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

评论(1

生活了然无味 2024-08-24 15:30:24

我没有 Windows 机器来真正检查这一点,但我认为您需要创建一个类型映射来告诉 swig 如何将 PyObject 包装器转换为 WNDPROC:

// assuming the wrapped object has an attribute "pointer" which contains 
// the numerical address of the WNDPROC
%typemap(in) WNDPROC {
    PyObject * addrobj = PyObject_GetAttrString($input, "pointer");
    void * ptr = PyLong_AsVoidPt(addrobj);
    $1 = (WNDPROC)ptr;
}

I don't have a windows machine to really check this, but I think you need to create a typemap to tell swig how to convert the PyObject wrapper to a WNDPROC:

// assuming the wrapped object has an attribute "pointer" which contains 
// the numerical address of the WNDPROC
%typemap(in) WNDPROC {
    PyObject * addrobj = PyObject_GetAttrString($input, "pointer");
    void * ptr = PyLong_AsVoidPt(addrobj);
    $1 = (WNDPROC)ptr;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文