Is it possible to share an enum declaration between C# and unmanaged C++?

后端 未结 5 1546
遥遥无期
遥遥无期 2020-12-05 07:39

Is there a way to share an enum definition between native (unmanaged) C++ and (managed) C#?

I have the following enum used in completely unmanaged code:



        
相关标签:
5条回答
  • 2020-12-05 08:21

    Thanks for sharing!

    I played around a bit and found a way to have multiple enum and constant declarations without having tons of extra lines :)

    // This is a valid C, C++ and C# file :)
    #if __LINE__
    #define public
    #else
    namespace MyCSharpNamespace{
    #endif
    
        public enum MyConstant { MaxStr = 256 };
        public enum MyEnum1{ MyEnum1_A, MyEnum1_B };
        public enum MyEnum2{ MyEnum2_A, MyEnum2_B };
    
    #if __LINE__
    #undef public
    #else
    }
    #endif
    

    Remember to name your file *.cs

    0 讨论(0)
  • 2020-12-05 08:25

    You can use a single .cs file and share it between both projects. #include in C++ on a .cs file should be no problem.

    This would be an example .cs file:

    #if !__LINE__    
    namespace MyNamespace
    {
        public 
    #endif
    
    // shared enum for both C, C++ and C#
    enum MyEnum { myVal1, myVal2 };
    
    #if !__LINE__
    }
    #endif
    

    If you want multiple enums in one file, you can do this (although you have to temporarily define public to be nothing for C / C++):

    #if __LINE__
    #define public
    #else
    namespace MyNamespace
    {
    #endif
    
        public enum MyEnum { MyEnumValue1, MyEnumValue2 };
        public enum MyEnum2 { MyEnum2Value1, MyEnum2Value2 };
        public enum MyEnum3 { MyEnum3Value1, MyEnum3Value2 };
    
    #if __LINE__
    #undef public
    #else
    }
    #endif
    
    0 讨论(0)
  • 2020-12-05 08:33

    You can expose the C# library to COM and then import the type library into the unmanaged code - this way you will be able to use the enum defined in C# library in the unmanaged library.

    0 讨论(0)
  • 2020-12-05 08:38

    Unmanaged C++ and C# live in two different worlds, so no there is no way to use the same enum, without changing the c++ DLL into a managed one.

    And even then, you'd probably need the duplication in the managed C++ DLL.

    A C++ enum is much like a list of constants, whereas a C# enum inherits the Enum class, and thus provides quite a few "tricks". So as you can see, they're very different.

    If it doesn't matter whether the native C++ DLL is native or managed, I'd turn it into a managed one and wrap the native calls inside a managed C++ Layer.

    That way you can have the enum duplication inside the C++ DLL, and also you can get rid of all the interop at the same time :-)

    0 讨论(0)
  • 2020-12-05 08:39

    I've had the same problem in the past and solved it using preprocessor definitions.

    In your unmanaged code, inside a header that can also be included by your managed wrapper, place your enumeration items into a #define.

    Then, in your managed and unmanaged enumeration definitions, use the same #define for both usages.

    The movement of the enumerations between the managed and unmanaged world looks slightly nasty (basically a cast is needed), but to your caller in the unmanaged world, it'll look and feel fine.

    Good luck,

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题