views:

83

answers:

5

Hello ; I am very new to C#, and i am trying to use a help package for my project. The package is written in c and has 1) /bin/ several .dll files 2) /include/ has a header file 3) /lib/msvc/ .lib file my question is how can i use those files in my C# WPF project? i know there is no "#include" in C#, and the .dll can not be imported by adding to the project's reference. so how can i do it in C#?

Thanks

+3  A: 

You need to add a series of C# Platform Invocation method definitions. This tells C# how to call into the .dll, and use the C API directly.

The header and library files are completely unused.

Reed Copsey
+1 for pointing out that the header and library files are not used in PInvoke interop (though a human needs to use the header to generate the C# declarations.)
Kate Gregory
A: 

You need to use pInvoke

http://msdn.microsoft.com/en-us/library/aa288468%28VS.71%29.aspx

Zippit
+1  A: 

You might find managed C++ useful. You can write a managed C++ library that uses the header files and the .lib directly, and wraps them with a set of .NET classes to be used by C#.

Tim Robinson
+2  A: 

Just adding to the answers, you may want to take a look in this blog post. It has a link to a Visual Studio addin that can generate P/Invoke signatures from your headers.

Best

Vagaus
+1, included in that post is a link to the P/Invoke Interop Assistant, extremely handy: http://clrinterop.codeplex.com/releases/view/14120
Mark
A: 

There are a lot of ways to do this "interop" - I once did a talk called "head spinning interop". The key is the structure of your native DLL. Since you say it's in C, the chances are it's in perfect shape to be used with PInvoke, which works best with C functions or C-style functions in C++. The other answers have excellent pointers to the syntax and some help to get your function (and the parameters it takes) declared on the C# side.

The other two main options are a C++/CLI wrapper, a good choice when the functions take a lot of complicated types, or COM, which would almost certainly require you to change the native code so is not a good fit in this situation.

Kate Gregory