简体   繁体   English

用Boost.Python包装和传递HWND

[英]Wrapping and Passing HWND with Boost.Python

I've created a Boost.Python wrapper (using Py++) for a C++ legacy class that takes a HWND window handle in its constructor. 我为C ++旧式类创建了Boost.Python包装器(使用Py ++),该类在其构造函数中使用HWND窗口句柄。 However, after exporting the module to python when I try to use it, I get a type mismatch error. 但是,当我尝试使用模块将其导出到python后,出现类型不匹配错误。

Here is the C++ class I'm wrapping: 这是我要包装的C ++类:

// File Foo.hpp
//
#include "Windows.h"
class Foo
{
public:
    Foo( const HWND window ){}

    virtual ~Foo(){}

    virtual int Bar( int num ) { return num; }
};   

The Py++ output: 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 )

The generated wrapper: 生成的包装器:

#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") ) );

        }
    }
}

In python I get the not-match error: 在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)
>>>

How can I correct this problem to be able to pass a window's handle (from win32gui) in Python to C++ class, and interact with it? 如何解决此问题,以便能够将Python中的窗口句柄(从win32gui)传递给C ++类,并与其进行交互?

Environment: Visual Studio 2008, Boost 1.44, gcc-xml 0.9.0, py++ 1.0.0, pygccxml 1.1.0 环境:Visual Studio 2008,Boost 1.44,gcc-xml 0.9.0,py ++ 1.0.0,pygccxml 1.1.0

You can't assume that the wrapped type produced by boost python will be compatible with the wrapping procedure used by another tool. 您不能假设boost python产生的包装类型将与其他工具使用的包装过程兼容。 In this case win32 gui is built using SWIG, which can introduce additional layers of C, C++ and python code (depending on the types to be wrapped) to produce correct code. 在这种情况下,win32 gui是使用SWIG构建的,它可以引入C,C ++和python代码的附加层(取决于要包装的类型)以生成正确的代码。

You can see the SWIG interface file for the GetDesktopWindow (and lots of other functions) in the pywin32 mercurial repository 您可以在pywin32 mercurial存储库中看到GetDesktopWindow的SWIG接口文件(以及许多其他功能)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM