how to read .h file in C#
I have one .Net assembly which includes native .h files as a link. The .h file is placed at a common loaction and used across projects (C++ and C#).
These header files have some structures and #define in it.
In my .Net assembly i have defined structures and doing marshalling using Interop services and is working fine. For #define also, i defined
variables in .Net assembly and setting the value, something like:
C++ .h file
#define MyValue 0x100
C# assembly
internal class MyClass
{
protected const MyValue = 0x100;
}
But with such code, i need to update MyValue in C# assembly everytime it gets modified in .h file. And generally in my project, the
structures in this .h file are not changed but the #define variable gets modified often.
Is there any way, that allows me
to set this variable directly from .h file, something like:
internal class MyClass
{
protected const MyValue = MyValue (from .h file);
}
i.e. is it possible to read .h file in C# assembly? Please let me know some ideas for the above problem.
Re: how to read .h file in C#
no. when you update your header files, update your C# interop files too.
the only way to update your C# interop files would be to create a pre-build event that parsed your header files and (deleted the existing C# file and) generated your C# files on every build.
Re: how to read .h file in C#
Is using COM an option here? You are basically manually doing what COM can do for you by exposing types and structures defined in the native dll (#defines will still be an issue though). If you can't go the COM route then yes, you will need to parse the files when they change, preferably in an automated fashion as MadHatter suggested.
Re: how to read .h file in C#
Using T4 for this may be a good idea?
Whenever the C++'s .h file changes, simply re-run the T4 to recreate the C# bits.