2012-04-30 35 views
2

我創建了一個Boost.Python的包裝(用py ++)一個C++遺留類,需要一個HWND窗口句柄在其構造函數傳遞HWND。但是,當我嘗試使用它時將模塊導出到python後,出現類型不匹配錯誤。包裝與Boost.Python的

這裏是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中,升壓1.44,GCC-XML 0.9.0,PY ++ 1.0.0,1.1.0 pygccxml

回答

0

你不能假設,通過升壓蟒蛇生產包裝類型將是兼容的與另一個工具使用的包裝程序。在這種情況下,win32 gui使用SWIG構建,它可以引入額外的C,C++和python代碼層(取決於要包裝的類型)以生成正確的代碼。

可以在pywin32請參閱GetDesktopWindow的SWIG接口文件(和許多其他功能)mercurial repository

+0

謝謝,但我怎麼能'GetDesktopWindow'的INT輸出轉換成C++的HWND(無效*)? – Ehsan