Re: Enum between Managed C++ and C#

  • Thread starter Thread starter Jay B. Harlow [MVP - Outlook]
  • Start date Start date
J

Jay B. Harlow [MVP - Outlook]

Drew,
If you make the enum public __value in your managed C++, it will be usable
in your C# code.

Something like:

public __value enum MyEnum
{
Value1 = 1,
Value2 = 2
};

The __value says that it is a managed type

Hope this helps
Jay
 
Drew,
Thanks Jay for the response. However, I want to prevent redefining my
enums. To go into more detail, here is the layout of my files.
Isn't this like wanting your cake and eating to? :-)

Are you saying that :

namespace ManagedClassLibrary
{
public __value enum BaseUnit
{
evBaseUnit1,
evBaseUnit2
};
public class __gc ManagedClass
{
void Open(BaseUnit eBaseUnit)
{
}
};
}

That Open does not show the enums for you in C#? I do the preceding in my
VB.NET/C++ project and I see the enums. I do not use a #define to hide the
__value. Remember the enum in C# is going to be prefixed with the namespace
you put them in. You did put them in a namespace? You also need to include
public.

I was going to suggest trying to use a #define to include or exclude the
__value from the enum, then cast as needed in your managed C++ wrapper. I
think first its best to make sure you see enums. Then worry about redefining
them...

Hope this helps
Jay

Drew said:
Thanks Jay for the response. However, I want to prevent redefining my
enums. To go into more detail, here is the layout of my files.

baseunits.h
===========
enum BaseUnit
{
evBaseUnit1,
evBaseUnit2
};

managed.cpp
===========

#include "baseunits.h"

class __gc ManagedClass
{
void Open(BaseUnit eBaseUnit)
{
}
};

csharp.cs
=========

ManagedClass mc;
mc.Open(int eBaseUnit); // This shows up as an integer

I can redefine a managed enum using public __value and just cast to
the native enum, but I do not want to redefine the enums. I've even
tried adding:

#ifdef DOTNET
public __value
#endif

to my baseunits.h, and defined DOTNET in my managed wrapper, but I
still cant see it in my csharp project.

Thanks,
Drew
"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
Drew,
If you make the enum public __value in your managed C++, it will be usable
in your C# code.

Something like:

public __value enum MyEnum
{
Value1 = 1,
Value2 = 2
};

The __value says that it is a managed type

Hope this helps
Jay
 
I love wanting my cake and eating it at the same time! ;)

To clarify, if I create a new set of enums (e.g. ManagedBaseUnit), I
can see them from C#. And as a matter of fact, casting the managed to
my unmanaged enums is exactly what I am doing right now. That works
fine. However, now I am maintaining two sets of enums. I've tried
everything I can think of to eliminate the second set of enums, but
it's not working. My enums are already in a namespace for my current
native C++ app.

Thanks for taking the time to help me out.

-Drew

Jay B. Harlow said:
Drew,
Thanks Jay for the response. However, I want to prevent redefining my
enums. To go into more detail, here is the layout of my files.
Isn't this like wanting your cake and eating to? :-)

Are you saying that :

namespace ManagedClassLibrary
{
public __value enum BaseUnit
{
evBaseUnit1,
evBaseUnit2
};
public class __gc ManagedClass
{
void Open(BaseUnit eBaseUnit)
{
}
};
}

That Open does not show the enums for you in C#? I do the preceding in my
VB.NET/C++ project and I see the enums. I do not use a #define to hide the
__value. Remember the enum in C# is going to be prefixed with the namespace
you put them in. You did put them in a namespace? You also need to include
public.

I was going to suggest trying to use a #define to include or exclude the
__value from the enum, then cast as needed in your managed C++ wrapper. I
think first its best to make sure you see enums. Then worry about redefining
them...

Hope this helps
Jay

Drew said:
Thanks Jay for the response. However, I want to prevent redefining my
enums. To go into more detail, here is the layout of my files.

baseunits.h
===========
enum BaseUnit
{
evBaseUnit1,
evBaseUnit2
};

managed.cpp
===========

#include "baseunits.h"

class __gc ManagedClass
{
void Open(BaseUnit eBaseUnit)
{
}
};

csharp.cs
=========

ManagedClass mc;
mc.Open(int eBaseUnit); // This shows up as an integer

I can redefine a managed enum using public __value and just cast to
the native enum, but I do not want to redefine the enums. I've even
tried adding:

#ifdef DOTNET
public __value
#endif

to my baseunits.h, and defined DOTNET in my managed wrapper, but I
still cant see it in my csharp project.

Thanks,
Drew
"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
Drew,
If you make the enum public __value in your managed C++, it will be usable
in your C# code.

Something like:

public __value enum MyEnum
{
Value1 = 1,
Value2 = 2
};

The __value says that it is a managed type

Hope this helps
Jay

I have an enum that I use throughout my native C++ code. Of course
when I use this enum in my managed C++ wrapper, everything is fine.
However, now I would like to use this enum in my C# code which calls
my managed C++ wrapper. Any ideas how? I don't want to have to copies
of my enum floating around (one for managed and one for unmanaged). I
also know the code will work just by passing in the appropriate
integer into my managed C++ class, but that's not good for users of my
managed C++ wrapper API.

Thanks,
Drew
 
Drew,
Its been a busy week...

I'm not sure what to offer, other than try to get the #include with #define
DOTNET __value working.

When you do the #include w/#define, is the #include in a namespace?

Hope this helps
Jay

Drew said:
I love wanting my cake and eating it at the same time! ;)

To clarify, if I create a new set of enums (e.g. ManagedBaseUnit), I
can see them from C#. And as a matter of fact, casting the managed to
my unmanaged enums is exactly what I am doing right now. That works
fine. However, now I am maintaining two sets of enums. I've tried
everything I can think of to eliminate the second set of enums, but
it's not working. My enums are already in a namespace for my current
native C++ app.

Thanks for taking the time to help me out.

-Drew

"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
Drew,
Thanks Jay for the response. However, I want to prevent redefining my
enums. To go into more detail, here is the layout of my files.
Isn't this like wanting your cake and eating to? :-)

Are you saying that :

namespace ManagedClassLibrary
{
public __value enum BaseUnit
{
evBaseUnit1,
evBaseUnit2
};
public class __gc ManagedClass
{
void Open(BaseUnit eBaseUnit)
{
}
};
}

That Open does not show the enums for you in C#? I do the preceding in my
VB.NET/C++ project and I see the enums. I do not use a #define to hide the
__value. Remember the enum in C# is going to be prefixed with the namespace
you put them in. You did put them in a namespace? You also need to include
public.

I was going to suggest trying to use a #define to include or exclude the
__value from the enum, then cast as needed in your managed C++ wrapper. I
think first its best to make sure you see enums. Then worry about redefining
them...

Hope this helps
Jay

Drew said:
Thanks Jay for the response. However, I want to prevent redefining my
enums. To go into more detail, here is the layout of my files.

baseunits.h
===========
enum BaseUnit
{
evBaseUnit1,
evBaseUnit2
};

managed.cpp
===========

#include "baseunits.h"

class __gc ManagedClass
{
void Open(BaseUnit eBaseUnit)
{
}
};

csharp.cs
=========

ManagedClass mc;
mc.Open(int eBaseUnit); // This shows up as an integer

I can redefine a managed enum using public __value and just cast to
the native enum, but I do not want to redefine the enums. I've even
tried adding:

#ifdef DOTNET
public __value
#endif

to my baseunits.h, and defined DOTNET in my managed wrapper, but I
still cant see it in my csharp project.

Thanks,
Drew
"Jay B. Harlow [MVP - Outlook]" <[email protected]> wrote in
message news: said:
Drew,
If you make the enum public __value in your managed C++, it will be usable
in your C# code.

Something like:

public __value enum MyEnum
{
Value1 = 1,
Value2 = 2
};

The __value says that it is a managed type

Hope this helps
Jay

I have an enum that I use throughout my native C++ code. Of course
when I use this enum in my managed C++ wrapper, everything is fine.
However, now I would like to use this enum in my C# code which calls
my managed C++ wrapper. Any ideas how? I don't want to have to copies
of my enum floating around (one for managed and one for unmanaged). I
also know the code will work just by passing in the appropriate
integer into my managed C++ class, but that's not good for users of my
managed C++ wrapper API.

Thanks,
Drew
 
Jay said:
I'm not sure what to offer, other than try to get the #include with
#define DOTNET __value working.

The only thing I would change is using the _MANAGED macro instead of DOTNET.
The compiler will define _MANAGED when compiling /clr in managed regions.

Other than that, I would need to know more details than that to help. I can
say that in future versions of Visual C++, we will solve this problem. I
know that's not much help now, but at least I know the pain here.

Cheerio!
 
Back
Top