简体   繁体   English

将C ++ DLL添加到C#项目中

[英]Adding C++ DLL's to a C# project

I'm trying to use the lame_enc.dll file from LAME in a C# project, but adding the thing seems impossible. 我正在尝试在C#项目中使用LAME的lame_enc.dll文件,但添加该东西似乎是不可能的。

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. 我一直收到一条错误消息,指出无法添加引用,请检查是否可访问,有效的程序集或COM组件。

I have no C++ experience, though I would like to use the functionality. 我没有C ++经验,但我想使用这个功能。 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. 现在我正在使用.NET框架中的Process来调用lame.exe并做一些事情,但我想知道是否还有另一种方法。

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. 在这种情况下我通常做的是将其添加为ressource而不是“copy local”设置。 That way the DLL is tied to and deployed with your project. 这样,DLL就与您的项目绑定并一起部署。 I then use DllImport to manually get the APIs I need from that DLL. 然后我使用DllImport从该DLL手动获取我需要的API。

You have to use P/Invoke to call unmanaged APIs from managed code. 您必须使用P / Invoke从托管代码调用非托管API。

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). 要在C#中使用非托管dll(本机C ++),您必须使用DllImport,而不是在visual studio中添加对项目的引用(这就是您收到错误的原因)。

Here is the documentation of DllImport from the MSDN. 以下是MSDN中DllImport的文档。

You will need to use PInvoke to call functions in your native lame dll. 您需要使用PInvoke来调用本机lame dll中的函数。 However, you will only be able to call functions that have been exported as "C" style. 但是,您只能调用已导出为“C”样式的函数。

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: 您可以使用像“PInvoke Interop Assistant”这样的工具,它可以帮助您在计算PInvoke调用签名时从C#调用您的本机dll:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM