Solved

Sharing types between unmanaged and managed languages

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with consolidating excel files using VB.net 2 29
VB.NET 2008 - Windows Protected Your PC 7 41
How do i run a c++ file? 15 46
Get month and date in a format 4 31
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
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 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…

840 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