0

I'm using VS2010, I'm try to use a C# dll to get any enum member name in C++,

My C# dll source code:

namespace CSharpFuncion
{
    public class CSFun
    {
        public string GetEnumName(Enum en)
        {
            return Enum.GetName(typeof(Enum), en);
        }
    }
}

My C++ code

#using "CSharpFuncion.dll"
using namespace CSharpFuncion;
CSFun ^ csFun = gcnew CSFun;
cout << csFun->GetEnumName(MyTestEnum::E_A) << endl;

Error message:

cannot convert parameter from 'MyTestEnum'  to 'System::Enum ^'

How can I fix it?

DaveG
  • 491
  • 1
  • 6
  • 19
  • 1
    is this `c++/cli`? if so can you edit your title and the tags – EdChum Jun 01 '18 at 08:48
  • An important point is whether `MyTestEnum` is a managed or unmanaged enum – Malachi Jun 01 '18 at 08:55
  • What does your MyTestEnum look like? – Kakalokia Jun 01 '18 at 09:01
  • @Malachi you mean `enum class`? I can't use it in VS2010 – DaveG Jun 01 '18 at 09:02
  • @Ali `public enum MyTestEnum { E_A = 1, E_B = 2, };` – DaveG Jun 01 '18 at 09:04
  • @Malachi did I accidentally delete you comment? so if I want to declare a managed enum I need to use `__value `, and for using `__value ` I need change `/clr` to `/clr:oldSyntax`, that cause my C++ can't use C# dll – DaveG Jun 01 '18 at 09:14
  • I deleted my own comment, because I'm unable to bring up my Visual Studio to verify stuff right now and don't wanna mislead you. However what you're saying there is the direction I was going in – Malachi Jun 01 '18 at 09:15

2 Answers2

1

Rather than

public enum MyTestEnum
{
        E_A = 1,
        E_B = 2
};

You need to make it

public enum class MyTestEnum
{
        E_A = 1,
        E_B = 2
};

So just add the class keyword.

And change return Enum.GetName(typeof(Enum), en); to return en.ToString()

Kakalokia
  • 3,191
  • 3
  • 24
  • 42
  • 1
    It is worth noting that `enum class` was adopted by the C++11 standard. The accessibility specifier (`public` here) is now crucial to distinguish between a native and managed enum type. Beware that the OP's question is misleading, public is not valid on a native enum type. – Hans Passant Jun 01 '18 at 10:48
  • Thanks @HansPassant ... Yes that's correct indeed, forgot to add that remark. The syntax itself (public enum) is not valid C++ syntax anyway, so hopefully it's clear to the OP that this is a managed enum, not a native one. – Kakalokia Jun 01 '18 at 11:20
0

you have to give Enum.GetName(typeof(MyTestEnum ), 1); to get the name of value (E_A) in that enum

Test12345
  • 1,625
  • 1
  • 12
  • 21