我已经使用Py++为一个在其构造函数中接受HWND窗口句柄的C++遗留类创建了一个Boost.Python包装器。然而,在将模块导出到Python后,当我尝试使用它时,我会收到类型不匹配的错误。
这是我正在包装的C++类:
生成的包装器:
这是我正在包装的C++类:
// File Foo.hpp
//
#include "Windows.h"
class Foo
{
public:
Foo( const HWND window ){}
virtual ~Foo(){}
virtual int Bar( int num ) { return num; }
};
Py++的输出结果:
INFO Parsing source file "foo.hpp" ...
INFO gccxml cmd: ""c:\Program Files (x86)\gccxml 0.9\bin\gccxml.exe" -I"." "foo.hpp" -fxml="d:\temp\tmpdng3ts.xml""
INFO GCCXML version - 0.9( 1.127 )
INFO: file "generated_wrapper.cpp" - updated( 0.001607 seconds )
生成的包装器:
#include "boost/python.hpp"
#include "foo.hpp"
namespace bp = boost::python;
struct Foo_wrapper : Foo, bp::wrapper< Foo > {
Foo_wrapper(::HWND const window )
: Foo( window )
, bp::wrapper< Foo >(){
// constructor
}
virtual int Bar( int num ) {
if( bp::override func_Bar = this->get_override( "Bar" ) )
return func_Bar( num );
else{
return this->Foo::Bar( num );
}
}
int default_Bar( int num ) {
return Foo::Bar( num );
}
};
BOOST_PYTHON_MODULE(MyWrapper){
{ //::Foo
typedef bp::class_< Foo_wrapper > Foo_exposer_t;
Foo_exposer_t Foo_exposer = Foo_exposer_t( "Foo", bp::init< HWND__ *>(( bp::arg("window") )) );
bp::scope Foo_scope( Foo_exposer );
bp::implicitly_convertible< const HWND, Foo >();
{ //::Foo::Bar
typedef int ( ::Foo::*Bar_function_type )( int ) ;
typedef int ( Foo_wrapper::*default_Bar_function_type )( int ) ;
Foo_exposer.def(
"Bar"
, Bar_function_type(&::Foo::Bar)
, default_Bar_function_type(&Foo_wrapper::default_Bar)
, ( bp::arg("num") ) );
}
}
}
在 Python 中,我遇到了不匹配的错误:
>>> import MyWrapper
>>> import win32gui
>>> hwnd = win32gui.GetDesktopWindow()
>>> foo = MyWrapper.Foo(hwnd)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Foo.__init__(Foo, int)
did not match C++ signature:
__init__(struct _object *, struct HWND__ * window)
>>>
如何解决这个问题,以便能够在Python中传递一个窗口句柄(来自win32gui)到C++类,并与之交互?
环境: Visual Studio 2008,Boost 1.44,gcc-xml 0.9.0,py++ 1.0.0,pygccxml 1.1.0
GetDesktopWindow
的int输出转换为C++的HWND(void *)? - Ehsan