Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sharing types between unmanaged and managed languages

Posted on 2008-10-11
3
Medium Priority
?
681 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

927 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