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 th
I would consider using a .tt file to process the .h and turn it into a .cs file. Its very easy and the source files will then be part of your C# solution (meaning they will be refreshed as the .h file changes), can be clicked on to open in the editor, etc.
If you've only got 1 #define it might be a little overkill, but if you have a file full of them (eg a mfc resource.h file perhaps) then this solution becomes a big win.
eg: create a file, DefineConverter.tt and add it to your project, change the marked line to refer to your .h file, and you'll get a new class in your project full of static const entries. (note the input file is relative to your project file, set hostspecific=false if you want absolute paths).
<#@ template language="C#v3.5" hostspecific="True" debug="True" #>
<#@ output extension="cs" #>
<#@ assembly name="System.Core.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#
string input_file = this.Host.ResolvePath("resource.h"); <---- change this
StreamReader defines = new StreamReader(input_file);
#>
//------------------------------------------------------------------------------
// This code was generated by template for T4
// Generated at <#=DateTime.Now#>
//------------------------------------------------------------------------------
namespace Constants
{
public class <#=System.IO.Path.GetFileNameWithoutExtension(input_file)#>
{
<#
// constants definitions
while (defines.Peek() >= 0)
{
string def = defines.ReadLine();
string[] parts;
if (def.Length > 3 && def.StartsWith("#define"))
{
parts = def.Split(null as char[], StringSplitOptions.RemoveEmptyEntries);
try {
Int32 numval = Convert.ToInt32(parts[2]);
#>
public static const int <#=parts[1]#> = <#=parts[2]#>;
<#
}
catch (FormatException e) {
#>
public static const string <#=parts[1]#> = "<#=parts[2]#>";
<#
}
}
} #>
}
}