views:

85

answers:

2

Is it possible to compile C code into a Visual C++ dll? I'm looking at using some C code with a .Net project and trying to determine whether this is even an option.

Thanks, Becky

+2  A: 

yes. If you want to get rid of name mangling use "extern "C" { /*...*/ } construct.

Also, refer FAQ : How to mix C and C++

aJ
+1  A: 

Given that C++ is largely backward compatible with C, you should be able to recompile the code using the C++ compiler unless the code uses some C99 features. However, keep in mind that C++/CLI is not standard C++ so there might be additional issues.

As aJ said, if you want to avoid the name mangling, you'll have to 'extern C' the symbols.

Another way to accomplish this would be to leave the C library as standard native code and write a thin C++/CLI layer for it. Then expose the C++/CLI layer to your .NET application.

Timo Geusch
Do you have any resources you could point me to for the thin C++/CLI layer and exposing to .Net at all as I'm a bit of a C/C++ beginner :o)
Becky Franklin
If it's a bog-standard windows DLL compiled with a C compiler, then PInvoke should do it - http://msdn.microsoft.com/en-us/library/aa446536.aspx
Pete Kirkham
@Becky: A (very) basic introduction to C++/CLI is here: http://msdn.microsoft.com/en-gb/magazine/cc163681.aspx. In order to write a .NET layer, you use some of the above constructs like the CLI classes to create something .NET understands and directly call into your C code from there. The compiler will handle the transition between managed and unmanaged code.
Timo Geusch
@Pete, thanks for pointing this out, I tend to forget about PInvoke as a lot of the code I had to use was too complex to use via PInvoke.
Timo Geusch
Thanks very much - I now have a Xmas project in learning all this :o)
Becky Franklin