Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dean Mitchell - 02 Aug 2007 22:17 GMT
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
Sheng Jiang[MVP] - 03 Aug 2007 01:24 GMT
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.
Signature

Sheng Jiang
Microsoft MVP in VC++

> Hi everyone,
>
[quoted text clipped - 11 lines]
> Regards
> Dean Mitchell
Ben Voigt [C++ MVP] - 03 Aug 2007 18:01 GMT
> 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
[quoted text clipped - 19 lines]
> #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
};
Pixel.to.life - 03 Aug 2007 01:56 GMT
> Hi everyone,
>
[quoted text clipped - 11 lines]
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.