通过SWIG到C#使用多个dll [英] Using multiple dlls with SWIG to C#

查看:98
本文介绍了通过SWIG到C#使用多个dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个本机c ++ dll,例如A和B.类X驻留在A.dll中,类Y驻留在B.dll中,其接口需要 shared_ptr< X> .我想在C#应用程序中使用此接口实例化X并将其移交给Y.

I have 2 native c++ dlls, say A and B. Class X lives in A.dll, and class Y lives in B.dll, with an interface that requires a shared_ptr<X>. I'd like to instantiate X and hand it over to Y using this interface, within a C# app.

因为这两种类型存在于不同的dll中,所以我分别使用 swig -dllimport A -c ++ -csharp Ai swig -dllimport B -c ++ -csharp Bi .在Ai中,我使用了%shared_ptr(X)宏,从而允许我在所有需要 shared_ptr< X> 的接口中流畅地使用X类型的对象(前提是那些接口位于A.dll中.)

Because these two types live in different dlls, I build them separately using swig -dllimport A -c++ -csharp A.i, and swig -dllimport B -c++ -csharp B.i. Within A.i, I've used the %shared_ptr(X) macro allowing me to use an object of type X fluidly in all interfaces that require shared_ptr<X> (provided those interfaces are in A.dll).

我的问题是,如果B.dll中的类Y在其接口中采用 shared_ptr< X> 怎么办?我该如何使用B.i进行第二步的构建,以了解 shared_ptr< X> (位于A.dll中)?

My problem is, what if class Y in B.dll takes a shared_ptr<X> in it's interface? How can I make that second swig build, with B.i, aware of shared_ptr<X> (which lives in A.dll)?

推荐答案

您正在寻找的是 %import .您可以按以下方式使用它,例如与模块test1一起使用:

What you're looking for is %import in SWIG. You can use this as follows, for example with a module test1:

%module test1

%include <std_shared_ptr.i>

%shared_ptr(Foo);

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

可以从另一个模块test2中引用哪个:

Which can be referenced from another module, test2:

%module test2

%import "test1.i"

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

%import 的功能(简而言之)是读取接口,但 not 不会为此接口生成任何包装器代码.也就是说,它将知道它的内容,但不会因此而将任何东西直接输出到包装器中.

What %import does (in an nutshell) is to read the interface, but not generate any wrapper code for it. That is to say it will be aware of the contents of it but not directly output any thing into the wrapper because of it.

这篇关于通过SWIG到C#使用多个dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆