Adding C++ DLL's to a C# project
Asked Answered
I

4

14

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.

Intransigeance answered 22/5, 2010 at 19:2 Comment(1)
To add to these other comments, if you end up having to make some sort of wrapper, it may be useful to go whole-hog, and use a COM-callable wrapper. This way you could call it from, say, jscript as well :) And if you web search for LAME COM wrapper, you get an answer like this: mail-archive.com/[email protected]/msg02219.htmlSeminarian
R
6

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

Reprehend answered 22/5, 2010 at 19:31 Comment(0)
S
14

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.

Se answered 22/5, 2010 at 19:33 Comment(0)
R
6

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

Reprehend answered 22/5, 2010 at 19:31 Comment(0)
S
5

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.

Salta answered 22/5, 2010 at 19:28 Comment(0)
S
3

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

Suint answered 22/5, 2010 at 19:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.