none
How do I pass an enum type argument from a managed wrapper to a native function? RRS feed

  • Question

  •  

    I'm writing a wrapper for an unmanaged C++ lib that i can call in .NET.  In my native wrapper, say I have a widget that i create and one of its functions' parameters is an enum data type that is defined in the native code.  Obviously I can't reference the native enum in the managed code.  Do I have to basically re-implement the enum in .NET and pass it as an integer and then cast it back in the native wrapper?  Not really sure how else to do it.

     

    Here's the enum:

    enum MyType
    
    {
          A = 1, 
          B = 2 
    };

     

     

    Here's the native wrapper:

    #include "stdafx.h"
    
    extern "C" 
    {
     __declspec(dllexport) void __stdcall Widget_DoSomething(char_t* name, /*what do i pass here? */)
     {
      CWidget *Widget = new CWidget(name);
      Widget->DoSomething(/*This takes an enum.  what do i pass?*/);
     }
    }
    

     

     

    Here's the .NET wrapper:

     

    [DllImport("Native.dll", CharSet = CharSet.Unicode, EntryPoint = "Widget_DoSomething")]
    public static extern void Widget_DoSomething(string name, /*what do i pass here?*/);
    

     

     

    Thanks!


    Saturday, March 26, 2011 8:13 AM

Answers

  • Do I have to basically re-implement the enum in .NET and pass it as an integer and then cast it back in the native wrapper?  Not really sure how else to do it.

    Yea...sort of.  The C# side would look like this:

    enum MyType 
    {
     A = 1, 
     B = 2 
    };
    
    [DllImport("Native.dll", CharSet = CharSet.Unicode)]
    public static extern void Widget_DoSomething(string name, MyType t);
    

     

    On the native side, the C function declaration can just use the C enum as the parameter type.  You won't need a typecast on that side since C enums are integers anyway and that's how the marshaller will send your C# enum value to the function by default.

    HTH,
    ShaneB


    • Marked as answer by BaggaDonuts Monday, March 28, 2011 6:34 AM
    Sunday, March 27, 2011 9:43 PM

All replies

  • Do I have to basically re-implement the enum in .NET and pass it as an integer and then cast it back in the native wrapper?  Not really sure how else to do it.

    Yea...sort of.  The C# side would look like this:

    enum MyType 
    {
     A = 1, 
     B = 2 
    };
    
    [DllImport("Native.dll", CharSet = CharSet.Unicode)]
    public static extern void Widget_DoSomething(string name, MyType t);
    

     

    On the native side, the C function declaration can just use the C enum as the parameter type.  You won't need a typecast on that side since C enums are integers anyway and that's how the marshaller will send your C# enum value to the function by default.

    HTH,
    ShaneB


    • Marked as answer by BaggaDonuts Monday, March 28, 2011 6:34 AM
    Sunday, March 27, 2011 9:43 PM
  • works great.  thanks much!
    Monday, March 28, 2011 6:34 AM