2012-11-30 118 views
2

我有2個本地C++ dll,說A和B.類X生活在A.dll中,類Y生活在B.dll中,需要一個接口shared_ptr<X>。我想實例化X並在C#應用程序中使用此接口將其交給Y.使用多個DLL與SWIG到C#

由於這兩種類型生活在不同的DLL中,我使用swig -dllimport A -c++ -csharp A.iswig -dllimport B -c++ -csharp B.i分別構建它們。在A.i中,我使用了%shared_ptr(X)宏,允許我在所有需要shared_ptr<X>(如果這些接口在A.dll中)的接口中流暢地使用類型X的對象。

我的問題是,如果B.dll中的類Y在其接口中使用shared_ptr<X>會怎麼樣?我怎麼能讓B.i知道shared_ptr<X>(生活在A.dll中)?

回答

3

SWIG正在尋找的是%import

%module test1 

%include <std_shared_ptr.i> 

%shared_ptr(Foo); 

%inline %{ 
    struct Foo {}; 
    std::shared_ptr<Foo> foo() { 
    return std::make_shared<Foo>(); 
    } 
%} 

可以從另一個模塊,TEST2引用:

%module test2 

%import "test1.i" 

%inline %{ 
    void bar(std::shared_ptr<Foo>) { 
    } 
%} 

確實(在簡單地說)是什麼%import讀你可以用一個模塊test1的如下使用,例如接口,但不是爲它生成任何包裝代碼。也就是說,它會意識到它的內容,但不會直接向包裝中輸出任何東西。

+0

正是我需要的!謝謝。 – Drism