Cython 包装 C++ WHND 转换指针

问题描述 投票:0回答:1

我尝试使用 Cython 包装一个简短的 C++ 函数(显示 Windows MessageBox),并向该函数传递一个指针 (int)(由 wxPython 生成)。

对该指针进行强制转换似乎无法正常工作,至少到达 C++ 级别的指针是不同的。

我哪里错了?

文件cpp_test-cpp

#include "cpp_test.h"

Test::Test() {
}

Test::~Test() {
}

int Test::Message(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, int uType) {
       printf("Test::Message Handle as seen from C++: %d\n", hWnd);
       return MessageBoxW(hWnd, lpText, lpCaption, uType);
}

文件cpp_test.h

#pragma once

#include <Windows.h>
#include <stdio.h>

class Test {
    public:
    Test();
    ~Test();
    int Message(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, int uType);
};

文件test.pxd

cdef extern from "Windows.h":
    ctypedef Py_UNICODE WCHAR
    ctypedef const WCHAR* LPCWSTR
    ctypedef void* HWND

cdef extern from "cpp_test.h":
    cdef cppclass Test:
        Test()
        int Message(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, int uType);

文件test.pyx

cimport test

cdef class pyTest:
    cdef Test* thisptr

    def __cinit__(self):
        print "__cinit__"
        self.thisptr = new Test()

    def __dealloc__(self):
        print "__dealloc__"
        del self.thisptr

    cpdef PyMessage(self, HandleToWindow):
        print "pyTest::PyMessage Handle before casting :" + str(HandleToWindow)
        if HandleToWindow == "NULL":
            title = u"Windows Interop Demo - Python"
            return self.thisptr.Message(NULL, u"Hello Cython \u263a", title, 0)
        else:
            hwnd =<HWND> HandleToWindow
            print "pyTest::PyMessage after recasting to object casting: " + str(<object>hwnd)
            title = u"Windows Interop Demo - Python"
            return self.thisptr.Message(hwnd, u"Hello Cython \u263a", title, 0)

文件useTest.py

from test import pyTest

k = pyTest()

print k.PyMessage(12345)
c++ cython
1个回答
1
投票

您的问题是,强制转换

hwnd =<HWND> HandleToWindow
获取指向您作为
PyObject
传递的
HandleToWindow
的指针,而不是根据
HandleToWindow
的内容设置一个 void 指针。

一个解决方案是创建一个 Cython 类

cdef class PyHandleToWindow:
   HWND ptr
   def __cinit__(self):
     self.ptr = NULL

然后使用它(在函数

PyMessage
中,以及任何需要在 Python 中传递这些句柄的其他地方)作为

cpdef PyMessage(self,handle_to_window):
  # code to deal with null goes here?
  hwnd = <PyHandleToWindow?>handle_to_window # note the question mark to test if the cast is valid
  return self.thisptr.Message(hwnd.ptr, u"Hello Cython \u263a", title, 0)

您还可以使用此方法直接传递 NULL 指针,而不是使用字符串。

© www.soinside.com 2019 - 2024. All rights reserved.