Adding C++ DLL's to a C# project

杀马特。学长 韩版系。学妹 提交于 2019-12-09 08:56:12

问题


I'm trying to use the lame_enc.dll file from LAME in a C# project, but adding the thing seems impossible.

I keep getting an error that says that a reference could not be added and to please check if the is accessible, a valid assembly or COM component.

I have no C++ experience, though I would like to use the functionality. Right now I'm using Process from the .NET framework to call lame.exe and do stuff, but I'd like to know if there's another way.


回答1:


You have to use P/Invoke to call unmanaged APIs from managed code.




回答2:


You can only add managed assemblies as a reference to a managed project. What I normally do in this situation is to add it as ressource instead with "copy local" settings. That way the DLL is tied to and deployed with your project. I then use DllImport to manually get the APIs I need from that DLL.




回答3:


To use an unmanaged dll (native C++) in C#, you have to use DllImport, not adding a reference to the project in visual studio (and that is why you get an error).

Here is the documentation of DllImport from the MSDN.




回答4:


You will need to use PInvoke to call functions in your native lame dll. However, you will only be able to call functions that have been exported as "C" style.

You can use a tool like "PInvoke Interop Assistant" that will help you when working out the PInvoke call signatures to make calls from C# to your native dll:

http://clrinterop.codeplex.com/releases/view/14120



来源:https://stackoverflow.com/questions/2889392/adding-c-dlls-to-a-c-sharp-project

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