Click to See Complete Forum and Search --> : Calling C functions using a C# project


cypriotpryde
March 19th, 2008, 10:44 AM
Hello all.

I I've been tasked with quite an interesting project right now, but I honestly don't even know where to start. I really need some input from some experienced .net developers.

The project I was assigned, was that I have to take a certain module of a project written in C for an embedded system, have it compile in a Windows environment, by itself (using some helper files to provide support for any calls to other modules), and create a C# Windows application that can be used to provide input to the C module, and observe its state and outputs afterwards (the module is a state machine).

Now, where I am right now, is that I've decided to use Visual C++ Express to create a project for the C code that I already have, and compile it using the Visual Studio C Compiler, so I shouldn't have a problem there, and I will be using Visual C# Express to create the windows application.

The question I have is, how would I call the necessary C functions in the C# project?

I am an experienced C# and C programmer, but I've never created any sort of project with outside dependencies and calls like this, especially in 2 different languages, so any input on the matter would be helpful. From a little prliminary research on the subject, I've seen that some people use DLLs or the PInvoke method, but I am unaware on how to use either, or if either will even be useful to my needs.

Once again, any input would be greatly appreciated. Thanks.

jasonli
March 19th, 2008, 11:38 AM
Maybe you could make a DLL to communicate data between C# project and C project.

nelo
March 19th, 2008, 11:49 AM
If you have the C code you can build a managed C++ library using Visual C++ Express. That will give a bonafide .NET assembly. You can add that assembly as a reference in your C# application. Provided you follow the rules (extra syntax) of managed C++ your assembly will look like any other .NET assembly as far as your C# application is concerned. The only tricky bit with the managed C++ is that it has extensive syntax to be able express managed and unmanaged data types within one assembly. The other alternative is the platform invoke which I think is nastier. You would only go for the platform invoke if you're given a compiled Win32 dll.

cypriotpryde
March 19th, 2008, 12:07 PM
hmmm... is it possible to make a c++ library using C code, though? Do I have to make a lot of changes to get it to be recognized as managed c++?

Right now, the project is about 10k lines of code, and all is written to MISRA C Coding standards.

nelo
March 19th, 2008, 07:23 PM
If you already have C code in a library (dll) then I would use platform invoke. It is possible to write C code against a C++ compiler. A c++ compiler would recognise classes and structs but you don't have to take advantage of C++. In fact in early days people still wrote C style (procedural as opposed to object oriented) code on C++ environments just out of habit...