Reuse define statement from .h file in C# code

c#, c++, c-preprocessor, header, versioning

Solution

You can achieve what you want in just a few steps:

- Create a MSBuild Task - http://msdn.microsoft.com/en-us/library/t9883dzc.aspx

- Update the project file to include a call to the task created prior to build

The task receives a parameter with the location of the header .h file you referred. It then extracts the version and put that version in a C# placeholder file you previously have created. Or you can think using AssemblyInfo.cs that normally holds versions if that is ok for you.

If you need extra information please feel free to comment.

Problem

I have C++ project (VS2005) which includes header file with version number in #define directive. Now I need to include exactly the same number in twin C# project. What is the best way to do it? I'm thinking about including this file as a resource, then parse it at a runtime with regex to recover version number, but maybe there's a better way, what do you think? I cannot move version outside .h file, also build system depends on it and the C# project is one which should be adapted.

Original source