Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 448
KdgDev Avatar asked May 22 '10 19:05

KdgDev


3 Answers

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.

like image 171
bitbonk Avatar answered Nov 13 '22 22:11

bitbonk


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

like image 6
Ranhiru Jude Cooray Avatar answered Nov 13 '22 21:11

Ranhiru Jude Cooray


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.

like image 4
Sameh Deabes Avatar answered Nov 13 '22 22:11

Sameh Deabes