The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Calling a C library from C#

Hi - though I'm quite comfortable with C# programming, I have to now call some functions in a C library, through C sharp. I'm having a bit of hard time figuring out how to do this and what I should look for/read. Any pointers appreciated.

The situation is this:
I have some C# code, and I have a bunch of functions which are in a library written in 'C'. I need to call those functions and get some output. What's the right/best way? (though I have the source code for C, converting that to C# is not an option)
C#-trouble
Tuesday, July 25, 2006
 
 
Meganonymous Rex Send private email
Tuesday, July 25, 2006
 
 
But the lib has to be a dll.  If it's not, just create a small dll wrapping the functions you need from the static library and then p/invoke that.
Meganonymous Rex Send private email
Tuesday, July 25, 2006
 
 
Meganonymous T-Rex, thanks for the links! I'm not too familiar with "creating a DLL around it", any pointers for that -- I'm a real newbie when it comes to making my C# code interop with any sort of native code... sorry if I sound stupid :(
C#-trouble
Tuesday, July 25, 2006
 
 
Libraries for use within C programs are provided either in static form (lib) or dynamic form (dll).  Although my knowledge of C# is limited, my understanding is that P/Invoke works like Java's JNI, and requires a DLL.

The weird looking stuff you write for the P/Invoke method call causes the runtime to load a DLL and execute a function from it.  You have to make sure to specify the right types for the arguments write, which is what Paul Yao's tool helps you do.

This is going to be aggravating for you at first but keep plugging. 

If the library you're linking to is a static library, then you will probably need to construct a C DLL that links to the static library.  You can't P/Invoke static libraries as far as I know.

What sort of library are you trying to call?  Is it a standard Windows DLL?  Is it one that was written in-house?
Meganonymous Rex Send private email
Tuesday, July 25, 2006
 
 
You might want to take a look at http://www.pinvoke.net for a repository of example p/invoke declarations for various Windows APIs.

Also, find somebody who knows C to work with you. C# and C are very, very different languages, and the p/invoke declaration is mainly about telling the CLR what the C code wants.
Chris Tavares Send private email
Tuesday, July 25, 2006
 
 
I do Windows C++ for a living.  Email me directly and I'll try to answer your questions.
Meganonymous Rex Send private email
Tuesday, July 25, 2006
 
 
Ho, stop!
Look here:
 http://www.codeproject.com/csharp/usecdlllibincs.asp

That's probably what you need
J. Send private email
Wednesday, July 26, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz