Solved

Sharing types between unmanaged and managed languages

Posted on 2008-10-11
3
658 Views
Last Modified: 2013-12-17
I have this project split into three parts:
1. Unmanaged C++ doing most of the internal processing
2. C# serving as UI
3. managed C++ bridging the two

Now I want simple types (enums and structs) defined in unmanaged C++ to be visible to C# project. and I don't want to copy-paste. when one definition changes I have to apply the changes to the other. and if I miss something I may not find it out until runtime.

So I came up with the following trick
(see the attached code)

The problem is that in C# I don't see any values in the Command enum. so in C# I cannot do things like
sendCommand(bridge.Command.MOVE_LEFT, ...); // won't compile

Although in managed C++ it compiles fine. (it doesn't show up in intellisense but that is a minor concern)
if (cmd == bridge::Command::MOVE_LEFT) // compiles

Why is the enum empty in C#?
Is there a better way to share types between managed and unmanaged environments without doing too much manual maintenance?
/** 1. define enums and structs in their own header files e.g. **/

enum Command {

    MOVE_LEFT,

    MOVE_RIGHT,

    etc.

};

 

/** 2. include it in both unmanaged and managed projects **/

/// unmanaged_cmd.h

namespace native_proj

{

    #include "Command.h"

}

 

/// managed_cmd.h (managed C++ bridge)

namespace bridge

{

    public

    #include "Command.h"

}

 

// at this point there should be two Command types native_proj::Command and bridge::Command

 

/** 3. use it in C# **/

namespace UI

{

    class derived_form : Form

    {

        void some_method()

        {

            sendCommand(bridge.Command.MOVE_LEFT, input_args, output_results);

        }

    }

}

Open in new window

0
Comment
Question by:jhshukla
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22694789
if following is the definition of the enum in the unmanaged c++ file then what will be the access modifier for that enum - as far as i remember it should be equivalent to internal in C# - in that case how can you access the type outside the assembly or interop in your case (i am just guessing as in C# it will not work outside the assembly as it will be given the default access modifier of internal)

enum Command {
    MOVE_LEFT,
    MOVE_RIGHT,
    etc.
};
0
 
LVL 9

Author Comment

by:jhshukla
ID: 22694918
that's why I have the following:

/// managed_cmd.h (managed C++ bridge)
namespace bridge
{
    public <<<<<<======== see this
    #include "Command.h"
}
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 0 total points
ID: 22694933
Just found the solution. see http://msdn.microsoft.com/en-us/library/a6cskb49.aspx for explanation

enum header files are now changed to look like
    MOVE_LEFT,
    MOVE_RIGHT,
    etc.

i.e. no enum type names or curly braces

and included like this:

// unmanaged:
namespace native_proj
{
    enum Command {
    #include "Command.h"
    };
}

// managed:
namespace bridge
{
    enum class Command { <<< note the difference "enum class" vs "enum"
    #include "Command.h"
    };
}
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now