How do I share a constant between C# and C++ code?

后端 未结 5 2171
鱼传尺愫
鱼传尺愫 2020-12-16 21:18

I\'m writing two processes using C# and WCF for one and C++ and WWSAPI for the second. I want to be able to define the address being used for communication between the two

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 21:38

    Wasn't happy with the other solutions for my use case so coded up a slightly hacky solution that seems to fit the original request better; a constant in one file that can be built into both a C# and a C++ project...

    1. Version information in a .cs file, in a common location.

    Like this:

    // Version.cs
    public static class MyAppVersion
    {
        //build
        public static string Number = "1.0";
        public static string Phase = "Alpha";
    
        //configuration (these are the build constants I use, substitute your own)
    #if BUILD_SHIPPING
        public static string Configuration = "Shipping";
    #elif BUILD_DEVELOPMENT
        public static string Configuration = "Development";
    #elif BUILD_DEBUG
        public static string Configuration = "Debug";
    #else
        "build type not defined"
    #endif
    }
    
    1. Include in C# project using Add Existing Item... [Add As Link]
    2. Include in C++ project (in a .cpp file) with a #include

    Like this:

    //include version information into a .cpp
    #define class namespace
    #define public
    #define static
    #define string const char*
    #include "..\..\Version.cs" //or to where-ever your file is
    ;
    #undef class
    #undef public
    #undef static
    #undef string
    
    1. Reference in C# with: MyAppVersion.Number
    2. Reference in C++ with: MyAppVersion::Number

提交回复
热议问题