Cython 包装 c++ WHND 铸造指针
Cython wrapping c++ WHND casting pointers
抱歉发布了那么多代码,但这是我能想到的显示问题的最小示例(好的,可以省略一些行,但这样应该可以测试了)
我尝试用 cyton 包装一个短的 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)
你的问题是演员 hwnd =<HWND> HandleToWindow
得到一个指向你作为 HandleToWindow
传递的 PyObject
的指针,而不是根据 [=14= 的内容设置一个空指针].
一个解决方案是创建一个 Cython class
cdef class PyHandleToWindow:
HWDB 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 指针,而不是使用字符串。
抱歉发布了那么多代码,但这是我能想到的显示问题的最小示例(好的,可以省略一些行,但这样应该可以测试了)
我尝试用 cyton 包装一个短的 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)
你的问题是演员 hwnd =<HWND> HandleToWindow
得到一个指向你作为 HandleToWindow
传递的 PyObject
的指针,而不是根据 [=14= 的内容设置一个空指针].
一个解决方案是创建一个 Cython class
cdef class PyHandleToWindow:
HWDB 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 指针,而不是使用字符串。