在 C# 应用程序中使用 C++ 类 DLL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/569603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using C++ Class DLL in C# Application
提问by cjserio
I have an unmanaged C++ DLL which merely exports a single class (not COM...it's just a simple C++ class) as its interface. I want to use this class in C# but am told that it cannot merely be imported into C#.
我有一个非托管的 C++ DLL,它只导出一个类(不是 COM...它只是一个简单的 C++ 类)作为它的接口。我想在 C# 中使用这个类,但被告知它不能仅仅导入到 C# 中。
What is the right way to use this class in my C# application?
在我的 C# 应用程序中使用这个类的正确方法是什么?
采纳答案by ShuggyCoUk
Simple way assuming class Foo:
假设类 Foo 的简单方法:
- Create a C++/CLI project, call this FooWrapper.
- Make FooWrapper depend on the unmanaged dll (however you normally would).
- Create a managed class ManagedFoo which contains a single private instance field of type Foo*.
- provide public wrapping functions in ManagedFoo which forward on to the underlying instance field.
- Optionally (though recommended):
- convert parameters from .net idioms (strings and the like) to C++ idioms (std::string or char*)
- catch unmanaged exceptions and throw managed ones instead
- 创建一个 C++/CLI 项目,将其命名为 FooWrapper。
- 使 FooWrapper 依赖于非托管 dll(但您通常会这样做)。
- 创建一个托管类 ManagedFoo,其中包含一个 Foo* 类型的私有实例字段。
- 在 ManagedFoo 中提供公共包装函数,这些函数转发到底层实例字段。
- 可选(虽然推荐):
- 将参数从 .net 习语(字符串等)转换为 C++ 习语(std::string 或 char*)
- 捕获非托管异常并抛出托管异常
Then you make your c# code depend on the FooWrapper project/dll and ensure that the unmanaged dll is properly deployed with it, how that is done depends on the unmanaged dll but in the same directory is normally sufficient.
然后你让你的 c# 代码依赖于 FooWrapper 项目/dll 并确保非托管 dll 正确部署,如何完成取决于非托管 dll 但在同一目录中通常就足够了。
If the functions do not rely on instances of the class then even simpler is P/Invoke
如果函数不依赖于类的实例,那么更简单的是 P/Invoke
回答by Richard
You need an proxy (GoF pattern) intermediary to bridge the managed/unmanaged boundary.
您需要一个代理(GoF 模式)中介来桥接托管/非托管边界。
Two options:
两种选择:
- A C++/CLI wrapper
- A COM wrapper.
- C++/CLI 包装器
- COM 包装器。
The former will be more direct, and latter has two steps pure C++ -> COM -> .NET.
前者会更直接,后者有两步纯C++ -> COM -> .NET。
回答by Stewart
This answer might be overkill for a single class library, but SWIG is a good solution for "wrapping" C/C++ classes for use from other languages. It works well with C#.
对于单个类库来说,这个答案可能有点过分,但 SWIG 是“包装” C/C++ 类以供其他语言使用的一个很好的解决方案。它适用于 C#。
回答by Steve
DllImport is your best bet. There is a bit of data type massaging, especially if you are passing structs, but you can do almost anything with it.
DllImport 是您最好的选择。有一些数据类型按摩,特别是当你传递结构时,但你几乎可以用它做任何事情。
回答by chrish
Sometimes, it is easier to provide your own C interface. SWIGis non-trivial to setup. I have use managed C++ and C++/CLI and they are fine. The easiest was just doing a C wrapper (and can be used by about any other language since most have a way to call a C function).
有时,提供自己的 C 接口更容易。 SWIG 的设置非常重要。我使用了托管 C++ 和 C++/CLI,它们很好。最简单的就是做一个 C 包装器(并且可以被任何其他语言使用,因为大多数语言都有调用 C 函数的方法)。