469,609 Members | 1,178 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Exposing a c++ enum in a managed c++ assembly

Hi everyone,

We have a c++ server application that we are writing a GUI client
application for. To save our time and to avoid duplicating all the code and
functionality that already exists in c++ classes I am building a managed c++
assembly around these classes so that a c# program can use them.

The problem is that alot of these c++ classes use enums, I would like to
make these visible to the c# application but I cannot find a way to do this.
Do I have to duplicate them so that I have one lot for C++ and the other for
dotnet? There must be an easier way?

Thanks for your time
Regards
Dean Mitchell

Aug 2 '07 #1
3 7661
you need to declare the enum using the enum class keyword, you can put the
enum entries in a #if block to generate the managed or native version of the
enum from the same file.
//enumsample.h
#if defined(_MANAGED)
enum class className
{
#else
enum {
#endif
.......
#if defined(_MANAGED)
}
#else
} className
#endif

//enumsample.cpp
#undef _MANAGED
#include "enumsample.h"
#defefine _MANAGED
#include "enumsample.h"

Another method is to create a type library and #import it from C++,
reference it from your managed projects.
--
Sheng Jiang
Microsoft MVP in VC++
"Dean Mitchell" <no*@this.addresswrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
Hi everyone,

We have a c++ server application that we are writing a GUI client
application for. To save our time and to avoid duplicating all the code
and
functionality that already exists in c++ classes I am building a managed
c++
assembly around these classes so that a c# program can use them.

The problem is that alot of these c++ classes use enums, I would like to
make these visible to the c# application but I cannot find a way to do
this.
Do I have to duplicate them so that I have one lot for C++ and the other
for
dotnet? There must be an easier way?

Thanks for your time
Regards
Dean Mitchell

Aug 3 '07 #2
On Aug 2, 2:17 pm, "Dean Mitchell" <n...@this.addresswrote:
Hi everyone,

We have a c++ server application that we are writing a GUI client
application for. To save our time and to avoid duplicating all the code and
functionality that already exists in c++ classes I am building a managed c++
assembly around these classes so that a c# program can use them.

The problem is that alot of these c++ classes use enums, I would like to
make these visible to the c# application but I cannot find a way to do this.
Do I have to duplicate them so that I have one lot for C++ and the other for
dotnet? There must be an easier way?

Thanks for your time
Regards
Dean Mitchell
Dean,

I assume the enums are used at the API level (the interface between
unmanaged/managed code), so whenever you want to transfer control from
managed app to an unmanaged C++ method, you have to pass one of these
enum values in too. I faced a similar situation, and had these
options:

[1] pass in int values, cast to the enumns on the unmanaged side (I
wont recommend it)
[2] wherever you expose and wrap the unmanaged classes by managed
wrappers, just add enums with the class. If the enums are within the
unmanaged classes' scope, you can wrap them as and with you wrap the
classes. No?

I went the second way.

Aug 3 '07 #3

"Sheng Jiang[MVP]" <sh*********@hotmail.com.discusswrote in message
news:Om**************@TK2MSFTNGP05.phx.gbl...
you need to declare the enum using the enum class keyword, you can put the
enum entries in a #if block to generate the managed or native version of
the
enum from the same file.
//enumsample.h
#if defined(_MANAGED)
enum class className
{
#else
enum {
#endif
......
#if defined(_MANAGED)
}
#else
} className
#endif

//enumsample.cpp
#undef _MANAGED
#include "enumsample.h"
#defefine _MANAGED
#include "enumsample.h"
Very close. Fixing the syntax mistakes and simplifying:

#if _MANAGED
#define ENUMKEYWORD enum class
#else
#define ENUMKEYWORD enum
#endif

ENUMKEYWORD enumName
{
A = 0,
B,
C
};

Aug 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Moe Green | last post: by
21 posts views Thread by Andreas Huber | last post: by
2 posts views Thread by tzvika.visman | last post: by
5 posts views Thread by M O J O | last post: by
5 posts views Thread by mmkhajah | last post: by
12 posts views Thread by Cmtk Software | last post: by
3 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.