Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use C headers for protocol in C#

I've got technical problem with a new C# program I'm developing. In this project I need to communicate with another none windows based system on a TCP/IP network. Al the software written on the other system is done in C and any other future development will be done in C/C++ aswell. The protocols are all done in C by another engineer and definitions of the protocols are all done using C typedef struct's defining all the variables and using memcpy to extract/put the data packets which works fantastic for C. All my protocols are supplied as C header files with all the typedef and struct's in them and any changes made to the protocol in the future will be done in the same way.

My question is, if there is any way to use them in C#?

I've tried to compile them as a class into a DLL library but not working cause C# can only use managed C dll's. If I try and compile as managed C class, it just becomes a mess due to the fact that there are many arrays in the protocol and because the C code has to conform to a bunch of mill specs, many of the variables have been typedef'd. Now I could go and redo all the structures in C# but that's going to take a lot of time and I'm going to have to redo it every time a change is made or something added to protocol. Not even to mention the danger of errors slipping in every time I do it.

How it worked with my C projects is that the other engineer would just supply me with the updated header files.

So is there any way to use those header files directly in C# or a automated conversion I can do every time the protocol is updated? Well I basically need to use this header file to extract the data from the data stream coming over the TCP/IP connection (without begin able to use memcpy)

Reason for using C# is because I use a lot of graphics in WPF and Visual C++ doesn't support WPF

Any help or suggestions would be much appreciated?

like image 890
gerharddvs Avatar asked Nov 16 '12 09:11

gerharddvs


1 Answers

I've once had to use C headers in C# to get definitions of marshalled structures sent via TCP/IP. The approach we used was parsing the header files by T4 Text Template. It's a somewhat lenghty task though, you have to write C parser good enough for your headers and use it to produce .cs file, so there is a lot of string mess. For us, it was a good enough solution, so it may help you as well.

Have a look at T4 here: http://msdn.microsoft.com/en-us/library/bb126445.aspx

like image 118
Honza Brestan Avatar answered Sep 22 '22 18:09

Honza Brestan