Solved

Sharing types between unmanaged and managed languages

Posted on 2008-10-11
3
671 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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