我用python创建了一个qwidget,由它生成的HWND是sip.voidptr。现在,如果我想发送HWND到一个c++代码(通过cython)创建一个表面在vulkan(与vkCreateWin32SurfaceKHR)我应该怎么做?
下面是一段类似于我的代码…
pyandcpp.pyx
cdef extern from 'AppInit.hpp':
cdef cppclass cpClass:
cpClass(int, HWND)# hinstance, hwnd
cdef class pyClass:
cdef cpClass *thisptr
def __cinit__(self, h, ptr):
#print(str(<HWND> ptr))
self.thisptr = new cpClass( h,<HWND> ptr)#int h, void* ptr
print('Done...')
cppfile.cpp
void cpClass::cpClass(int h, HWND ptr) {
createSurdace(h, ptr);
void cpClass::createSurface(int h, HWND ptr) {
//window.createWindowSurface(instance, &surface_);
VkWin32SurfaceCreateInfoKHR create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
create_info.hinstance = GetModuleHandle(nullptr); //tried 0 also
create_info.hwnd = ptr; //HWND>
if (vkCreateWin32SurfaceKHR(instance, &create_info, nullptr, &surface_)) {
throw std::runtime_error("failed to create surface!");
} else {
throw std::runtime_error("successfull in create surface!");
}
}
Main.py
class HelloTriangleApplication(QWidget):
def __init__(self, parent):
super(HelloTriangleApplication, self).__init__(parent)
self.parent = parent
pyClass(0, self.winId())
我试过<void*>转换sip。Voidptr到CPP Voidptr,但它不起作用。@user1051003在这里问了同样的问题,但是没有答案。
sip.voidptr
有一个__int__
方法,允许您将地址表示为Python整数。Python整数可以很容易地(在Cython中)转换为C整数,然后转换为指针类型。
比如
from libc.stdint cimport uintptr_t # unsigned int, big enough to hold a pointer
cdef void* convertSipVoidPtrToCVoid(sipObj):
return <void*><uintptr_t>(int(sipObj))