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

感情迁移 提交于 2019-12-17 15:44:19

问题


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:

enum MyEnum { myVal1, myVal2 };

Our application sometimes uses a managed component. That C# component gets the enum item values as ints via a managed C++ interop dll (from the native dll). (The interop dll only loads if the C# component is needed.) The C# component has duplicated the enum definition:

public enum MyEnum { myVal1, myVal2 };

Is there a way to eliminate the duplication without turning the native C++ dll into a managed dll?


回答1:


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



回答2:


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




回答3:


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.




回答4:


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 :-)




回答5:


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,



来源:https://stackoverflow.com/questions/954321/is-it-possible-to-share-an-enum-declaration-between-c-sharp-and-unmanaged-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!