I have some c headers, and a c lib that I'd like to import and use in a c# project. How can I do this?
Asked
Active
Viewed 285 times
2 Answers
4
Use [System.Runtime.InteropServices.DllImport]
attribute (P/Invoke):
[DllImport("dllname.dll")]
static extern void MyFunctionName();

Mehrdad Afshari
- 414,610
- 91
- 852
- 789
-
I don't have a dll, just a lib, and a few headers – Malfist Apr 03 '09 at 20:51
-
You'll need to build that lib into a dll then. You can't link the lib directly into your C# program that I'm aware of. – Joel Coehoorn Apr 03 '09 at 20:52
-
Can't you create a DLL from the lib in C? It's not possible to call static C libraries directly. You might also take a look at C++/CLI in that case. – Mehrdad Afshari Apr 03 '09 at 20:52
-
I don't know, I didn't compile it. I suppose I could, and see what happens. – Malfist Apr 03 '09 at 20:54
-
You may have to wrap it up in a dll. – ojblass Apr 03 '09 at 21:15
2
What Mehrdad said.
Additionally, welcome to the wonderful world of marshalling. P/Invoke.Net is your new best friend.

Joel Coehoorn
- 399,467
- 113
- 570
- 794