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.

C DLL from C#/.NET - newbie, please help!

Hi,
I have a C# project and I have some C code. This 'C' code is quite long and does some message parsing code (for protocols) and some other stuff that I have neither the time nor the expertise to convert to C#. Trying to call in an 'unmanaged'  code segment isn't an option either as there's too much stuff there (header files, definitions and what not).

Can I take this 'C' code, put it in a C++ project in MS VS.NET, build it as a DLL and then include it in my C# project and call the routines?

Can anyone spread some light to me on this? Is this reasonable? And how do I do this? (I mean calling DLL routines from C3 program). I'm unfamiliar with this DLL business...so please help. (Well, i'm OK technically - so any links/pointers will get me going..just that i'm pretty clueless at this point)
C# /.NET noob
Monday, August 08, 2005
 
 
You're on the right track. Make an ATL COM server from the C code (you can do this in VS2003) and then call the resultant DLL from .NET.
Sassy Send private email
Monday, August 08, 2005
 
 
Sassy, you just pushed me from "I don't know this" territory to "I 'REALLY' don't know this" territory. No idea what this ATL COM server thingy is - did some quick web checks, they keep referring to it as useful in web services etc. I don't need any web service, my program runs as console on windows, and all I want to do is use a big chunk of C code unmodified and call some routines from my C#.. is that really hard?

For e.g.

C# -  I want to create some byte stream
C# - I make some call xyz.makestream(); xyz is actually a C++  class that has my 'C' code and does a whole lot of things and returns a byte sequence, or XML - whatever
C# /.NET noob
Monday, August 08, 2005
 
 
I'm not sure why Sassy suggested creating an ATL COM server DLL.  That is one way to create a DLL, but it is not necessary.  You can create a DLL from your C source code and export any function you want from it.  There is plenty of info on this in the MSDN help (see ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vccore/html/_core_DLL_Topics.htm). 

Once you have your C source building as a DLL, the question then is how to call exported functions in a C DLL from you .Net app.  There are a ton of articles out there describing how to do this.  Paul Dilascia just wrote a nice article on this in his "C++ at work" column in the Sept issue of MSDN magazine.  Read it for all the details.  Basically, what you want to do is use P/Invoke.  Using P/Invoke allows you to declare the external functions of the C DLL as static methods of a class using the DllImport attribute to specify that the functions live in an external DLL.  Again, see the article for details.
fjlaga Send private email
Monday, August 08, 2005
 
 
I do what fjlaga said.  Simply create a DLL that exports the C functions you want to use, then DllImport these as static extern methods in C# and call them like you would any other C# static method. 

Search for things like DllImport, P/Invoke and the System.Runtime.InteropServices namespace for more info.

As a tip, I'd recommend using the Depends utility (which should be located somewhere in one of the VS.NET folders) to verify the names of the functions exported from the C DLL.
SomeBody Send private email
Monday, August 08, 2005
 
 
Just another thought.

Why can't I create a managed C++ DLL - with some class and put all my C code in it, and then can't I reference it from my C# proj and simply call the functions?

Do I need this P/Invoke and DLLImport etc. etc. then? I have all the C source so I have the freedom to build it in any way  I want.

Am I missing something here?
C# /.NET noob
Monday, August 08, 2005
 
 
What you are suggesting is close to the second approach recommened in the aforementioned article.  Basically it boils down to creating managed wrappers for your native code.  Typically, the managed wrapper class just wraps the C++ class and exposes only those functions from the C++ class that are needed.  With a little work you could do the same thing with your C code.  This approach is better suited to existing C++ classes you want to expose to managed code.  To me, the P/Invoke approach seems better for your existing C code (it may not be written in a way that is easily wrapped by a managed class).

BTW, the P/Invoke or Managed Wrapper Class is the easy part.  The hard part comes when you have to marshal parameters between native code and .NET code.  Simple types are handled pretty well by default, but if you have arrays or structs to deal with, there will be some work involved.  Again, refer to the documentation/articles for details on this.
fjlaga Send private email
Tuesday, August 09, 2005
 
 
The one thing to watch out for is name mangling when you are building a dll for p/invoking.  Use the "C" modifier on the extern declaration of the C function, eg:

#define EXTERN extern "C"
EXTERN SomeFunction()
{
  ...
}

If you don't then your C++ compiler may mangle the name and the C# loader will not be able to find the exported name in the dll.

Marshalling, as someone else posted, works well 99% of the time.  Usually the biggest mistake is not specifying the correct charset in the dllimport annotation -- if you can have you C functions work with Unicode strings it will make things much smoother.  Oh, and don't forget to pin memory if you expect your C functions to operate on the memory in subsequent calls, otherwise the garbage collector may take it away from you.
Consultant to the Evil Empire
Monday, August 15, 2005
 
 

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

Other recent topics Other recent topics
 
Powered by FogBugz