将 C++ .lib 和 .h 文件导入 C# 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18150396/
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
Import a C++ .lib and .h file into a C# project?
提问by Dan James Palmer
I have just started a C# project and want to import a C++ .lib and it's corresponding header (.h) file.
我刚刚开始了一个 C# 项目,想要导入一个 C++ .lib 及其对应的头文件 (.h)。
I've read various posts that all mention .dll, rather than .lib, which is confusing me.
我读过很多帖子都提到了 .dll,而不是 .lib,这让我很困惑。
The image below shows the .lib and .h file I'm referring to, all I've done is drag them into the project.
下图显示了我所指的 .lib 和 .h 文件,我所做的只是将它们拖到项目中。
Can anyone point me to a clearer explanation of how to go about doing this? I'm sure it can't be as hard as it seems.
任何人都可以向我指出如何进行更清晰的解释吗?我相信它不会像看起来那么难。
采纳答案by S. Larws
What you could do, is creating a C++/CLI wrapper and expose the functionality of the lib you want to use via your wrapper. The created wrapper dll you can easily reference in your C# project. This of course takes a little bit of work to create the managed/unmanaged wrapper, but will pay off in the long run.
您可以做的是创建一个 C++/CLI 包装器并通过您的包装器公开您想要使用的库的功能。您可以在 C# 项目中轻松引用创建的包装器 dll。这当然需要一些工作来创建托管/非托管包装器,但从长远来看会得到回报。
To create a managed C++ project select under the C++ project templates CLR and Class Library. Here you can link to your lib, use the header file the way you are used to.
要创建托管 C++ 项目,请在 C++ 项目模板 CLR 和类库下选择。在这里你可以链接到你的库,按照你习惯的方式使用头文件。
Next create a new class (ref class) and wrap your library in it. An example might look something like this:
接下来创建一个新类(引用类)并将您的库包装在其中。一个示例可能如下所示:
LibHeader.h
int foo(...);
You write a wrapper class like this: Header:
你写一个这样的包装类:标题:
Wrapper.h
public ref class MyWrapper
{
public:
int fooWrapped();
};
Your Implementation:
您的实施:
Wrapper.cpp
#include Libheader.h
int MyWrapper::fooWrapped()
{
return foo();
}
Namespaces and all the good stuff omitted for simplicity. Now you can use MyWrapper in your C# code just as easy as any other managed class. Of course when the interface of the lib gets more complicated you have to think about it a bit more, but it might help to separate the lib-code from your application. Hope to have shed some light on it.
为简单起见,省略了命名空间和所有好东西。现在,您可以像使用任何其他托管类一样轻松地在 C# 代码中使用 MyWrapper。当然,当 lib 的接口变得更复杂时,您必须更多地考虑它,但是将 lib 代码与您的应用程序分开可能会有所帮助。希望对此有所了解。
回答by Kirk Backus
This is, unfortunately, a non-trivial problem.
不幸的是,这是一个不平凡的问题。
The reason is primarily due to the fact that C++
is an unmanaged language. C#
is a managed language. Managed and unmanaged refers to how a language manages memory.
原因主要是因为它C++
是一种非托管语言。 C#
是一种托管语言。托管和非托管是指语言如何管理内存。
C++
you must do your own memory management (allocating and freeing),C# .NET Framework
does memory management with a garbage collector.
C++
您必须进行自己的内存管理(分配和释放),C# .NET Framework
使用垃圾收集器进行内存管理。
In your library code
在您的库代码中
You mustmake sure all of the places you call new
, must call delete
, and the same goes for malloc
and free
if you are using the C
conventions.
您必须确保您调用的所有地方都new
必须调用delete
,malloc
并且free
如果您使用C
约定也是如此。
You will have to create a bunch of wrapper classesaround your function calls, and make sure you aren't leaking any memory in your C++
code.
您必须围绕函数调用创建一堆包装类,并确保您的C++
代码中没有泄漏任何内存。
The problem
问题
Your main problem (to my knowledge) is you won't be able to call those functions straight in C#
because you can't statically link unmanaged code into managed code.
您的主要问题(据我所知)是您将无法直接调用这些函数,C#
因为您无法将非托管代码静态链接到托管代码中。
You will have to write a .dll to wrap all your library functions in C++
. Once you do, you can use the C#
interop functionality to call those functions from the dll.
您必须编写一个 .dll 来将所有库函数包装在 .dll 中C++
。完成后,您可以使用C#
互操作功能从 dll 调用这些函数。
[DllImport("your_functions.dll", CharSet = CharSet.Auto)]
public extern void your_function();
回答by Kirk Backus
It is 'as hard as it seems'. C++ and C# are ambivalent. The first has deterministic destruction, the second not. Now, you write C++/cli delaying the destruction to some finalizer called by the garbage collector working in it's own thread, introducing problems all over the place (thread safety, are C++ members (used in c++/cli) valid?, ...). Even worse the GC might suggest C++ objects being tiny (a pointer) and provoke a kind of memory leak (due to late deallocating tiny objects). Essentially you end up in writing a GC on top of the C++/cli GC to delete non C++/cli (!) objects in the main thread or another. All that is insane, ...
这“和看起来一样难”。C++ 和 C# 是矛盾的。第一个具有确定性破坏,第二个没有。现在,您编写 C++/cli 将销毁延迟到由垃圾收集器在其自己的线程中工作的垃圾收集器调用的某些终结器,从而在各处引入问题(线程安全,C++ 成员(在 c++/cli 中使用)是否有效?,... )。更糟糕的是,GC 可能会建议 C++ 对象很小(指针)并引发一种内存泄漏(由于延迟释放微小对象)。本质上,您最终会在 C++/cli GC 之上编写 GC 以删除主线程或其他线程中的非 C++/cli (!) 对象。这一切都是疯狂的,...