2

I wanted to marshal a struct in C++ to use it in C#. It contains nested classes inside. when I debug I get the data except for the nested classes. I am novice in custom marshalling but would be glad if someone can help me on how to get going. I have read lot of articles but they all use simple examples line int and marshal them. Custom marshalling would help me a lot as i have many structs like this to be converted.

http://blogs.msdn.com/b/jaredpar/archive/2005/07/11/437584.aspx --this example uses ints and I am struck at my first char* []

my c++ structs:

struct device
{    
    char Name[32];
    char Port;
    int type;   
    double Extention;
    int Buttons[8];
    double Matrix[4][4];        
    unsigned char expand[24];
}

struct InitializationParameters
{           
    char *SATELLITE[8]; 
    double TOLERANCE;   
    BOOL AVERAGE_MODE;
    int Use_DRF;
    double BaseMatrix[4][4];        
    unsigned char expand[24];       
    Device MARKER[8];   
};

my C# class looks like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class  InitilizationParameters
{
    [MarshalAs(UnmanagedType.ByValArray,SizeConst = 8)]
    public string[] SATELLITE=new string[8];

    public double TOLERANCE;

    [MarshalAs(UnmanagedType.Bool)]
    public bool AVERAGE_MODE;

    public int Use_DRF;

    [MarshalAs(UnmanagedType.ByValArray,SizeConst = 4 * 4)]
    public double[,] BaseMatrix=new double[4,4];       

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
    public byte[] expand=new byte[24];

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public Device[] MARKER=new Device[8];       
}

Above marshaling gives me all data in C++ method when used with P/Invoke except for the nested class types. The above class is also marshaled similarly

  • You omitted two of the C# types. Could you cut this down to a minimum reproduction? You surely can demonstrate the problem with a type with a single member. – David Heffernan Mar 11 '15 at 14:11
  • Removed duplicate datatypes and additional class for brevity. – Sasi Bhushan Gotlagunta Mar 11 '15 at 14:22
  • We still cannot see how you translated the two other types. If you made them classes, then they will be marshalled as pointers. You need to translate the nested classes as structs. Probably for consistency best to do them all as structs. We also don't know which way the data flows. From managed to unmanaged, or vice versa. A complete C# program, and a complete C++ function with complete C++ types would help. – David Heffernan Mar 11 '15 at 14:24

0 Answers0