Referencing a Native C++ DLL in a C# Class Library Project in Visual Studio [duplicate]

老子叫甜甜 提交于 2019-12-13 01:17:03

问题


I have a C# Class Library Project in Visual Studio 2015 that creates a C# DLL. I use this plugin for various Unity Projects.

There are some math related functions that I have written in Native C++, which I want to access in my above mentioned Class Library Project. Is it possible to create a C++ plugin for the Class Library Project, which will finally be built into the C# DLL?

If not, what are other ways to use C++ code inside a C# Class Library Project?


@Jonathon Reinhart, I do not think this is a duplicate because in your provided link the user is trying to use C++ in a C# executable project. I am trying to create a C# DLL that uses a C++ DLL and is trying to ask if this was possible, and how to do it.


回答1:


There are 3 ways to use any existing DLL in any .NET Programm. In order of descending preference:

.NET DLL's can just be added to the project references. Indeed .NET executeables can be added the same. The internal structure is very similar to a .NET DLL. This behavior is comparable to Java bytecode adn executeables.

COM Interop. COM is bit of a predecessor for .NET. Not only can you use COM stuff in .NET, .NET DLL's carry COM interface stuff with them by default. Of course using this opens you to all the additional issues of using COM. It was a predecessors for a reason.

P/Invoke. The plusside is that you can use any old native DLL or Windows API. THe downside is that you work with all the limits of them. Unfitting binarities will get in the way. And often the best solution is to wrap the native DLL's into a helper process and use Interprocess Communicaation to talk with it.

So you get to pick your poision: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/



来源:https://stackoverflow.com/questions/53179829/referencing-a-native-c-dll-in-a-c-sharp-class-library-project-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!