5

How can I add a C++ DLL file in my .NET application?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mony
  • 51
  • 1
  • 2

4 Answers4

6

You would use an "extern" function, marked with the DllImport attribute.

[DllImport(@“C:\mylib.dll”)]
public static extern int myFunc(int param);
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
6

Depending on the nature of the DLL, you can

  1. Add a reference to a registered COM DLL,

  2. Call Win32 DLLs with P/Invoke, or

  3. Write a wrapper in C++/CLI.

dtb
  • 213,145
  • 36
  • 401
  • 431
0

If it's registered in COM, you can simply add a COM reference in Visual Studio and Visual Studio will do all the Interop creation for you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve Danner
  • 21,818
  • 7
  • 41
  • 51
0

Assuming you use Visual Studio, in your Solution right click "references" and choose "Add Reference". Select your dll file.

In the classes that will use the dll, add : using MyLibrarysName;

then you can call the functions in that DLL using Mylibraryname.myfunction

Roast
  • 1,715
  • 5
  • 24
  • 32
  • Yeah... thats exactly how to do it in simple step by step instructions. I dont understand why anyone would vote that down. +1 – StingyJack Feb 08 '10 at 18:17
  • If the DLL is a C++ dll, add reference doesn't allow to add this reference. The error report says: "A reference to ''could not be added. Please make sure that the file is accessible and that it is a valid assembly or COM component.' The standard C++ value doesn't have an assembly, nor is it a COM component – Harald Coppoolse Jun 19 '14 at 14:55