6

I have a header file and a .lib file created using C++.

Now I want to use it in a C# program. Is this possible?

Sam
  • 7,157
  • 15
  • 45
  • 65
user2389323
  • 759
  • 2
  • 9
  • 21
  • possible duplicate of [How to use \*.lib file in C# application?](http://stackoverflow.com/questions/2848193/how-to-use-lib-file-in-c-sharp-application) – tafa Jul 25 '13 at 13:04

4 Answers4

8

You can create a managed wrapper, see step by step instruction here:

http://tom-shelton.net/?p=95

Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199
1

There's not a traditional linker to let you import the lib. Your best bet is to compile to a COM library and use interop to use it.

Cj S.
  • 1,155
  • 8
  • 13
1

Not directly. You can interoperate with unmanaged DLLs via P/Invoke, or mixed-mode assemblies using C++/CLI. Either way, you'll have to create a wrapper project, or recompile the original .lib (if you have the sources) into DLL.

jlew
  • 10,253
  • 1
  • 32
  • 56
1

I don't know about a .lib file. But I do know if you compile your code as a DLL you can consume it as unmanaged code.

To do this you'll need to reference

System.Runtime.InteropServices

and you will need to define the method you want to use and give it the DllImport attribute. Something like this:

[DllImport("MyCPPDll.dll")]
public void SomeMethod(int someParameter);

Here are a few links that should help point you in the right direction:

http://msdn.microsoft.com/en-us/library/26thfadc(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(v=vs.100).aspx

sbrauen
  • 113
  • 5