Solved

Sharing types between unmanaged and managed languages

Posted on 2008-10-11
3
664 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

775 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