Generate .NET types from C header file?

  • Thread starter Thread starter andrew queisser
  • Start date Start date
A

andrew queisser

Hi all,

The question of reusing C header files in C# comes up frequently and there's
really no general solution. I do have a specific problem that I think can be
solved and I'd like to get some feedback on.

I have a C# app that communicates with an embedded C app via UDP packets. I
have to ensure that both sides see the exact same data structure. I know how
to do this by defining classes with attributes in C# and defining the
appropriate header and compiler switches on the embedded system.

I'm wondering if I can have a single source that defines the structure for
both systems. Right now I can envision the following solutions:

1) Write a utility that parses the C header and generates the C# class
definition.
2) Write a utility that parses some other file format (XML, etc.) and
generates both the header and the C# class definition
3) Use C++/CLI to somehow generate a .NET type that gets imported into the
C# app.

Solutions 1) and 2) are doable but seem kind of tedious and boring. Anyone
know if tools like this exist? The C headers have not been written yet and
they will be quite simple. No need to parse exotic C constructs.

Solution 3) seems a little more interesting but is it doable?

Thanks for any comments,
Andrew Queisser
 
andrew queisser said:
I'm wondering if I can have a single source that defines the structure for
both systems. Right now I can envision the following solutions:
[snip]

I have no solutions, but have you thought of

4) write the class definitions in C#, and then write C# reflection
code to examine them and generate the .h files for C. You reflection
code will throw exceptions if finds any non-.h-able C# things.
 
Lucian Wischik said:
andrew queisser said:
I'm wondering if I can have a single source that defines the structure for
both systems. Right now I can envision the following solutions:
[snip]

I have no solutions, but have you thought of

4) write the class definitions in C#, and then write C# reflection
code to examine them and generate the .h files for C. You reflection
code will throw exceptions if finds any non-.h-able C# things.

Ja, thanks, that might be better. I think this will become my default route
for now.

Andrew
 
Back
Top