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

Re: Generate .NET types from C header file? by Lucian

Lucian
Wed Apr 12 14:15:39 CDT 2006

"andrew queisser" <andrewdotqueisser@hp.com> wrote:
>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

Re: Generate .NET types from C header file? by andrew

andrew
Wed Apr 12 14:31:49 CDT 2006


"Lucian Wischik" <lu.nn@wischik.com> wrote in message
news:ubkq325abvjmsfrvb9dmc7e223i1ct2tcr@4ax.com...
> "andrew queisser" <andrewdotqueisser@hp.com> wrote:
>>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