Marshaling a C++ two-dimensional fixed length char array as a structure member

前端 未结 4 1389
Happy的楠姐
Happy的楠姐 2021-01-13 17:03

I am trying to call an unmanaged C++ function, that has a structure as an input parameter. The structure is defined in the header file like this:

struct MySt         


        
相关标签:
4条回答
  • 2021-01-13 17:33

    Try using multiple C# structs:

    [StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public struct MyStruct_Name
    {
        [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 25)]
        public string name;
    }
    
    [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct MyStruct
    {
        public int siOrder;
    
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6)]
        public MyStruct_Name aaszNames;
    
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.I4)]
        public int[] siId;
    
        [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.I4)]
        public int[] siTones;
    }
    

    This is how I've been passing arrays of C-style strings around.

    Don't forget to create the contents of aaszNames! The marshaller hates null references.

    MyStruct foo = new MyStruct();
    for (int i = 0; i < 6; i++)
    {
        foo.aaszNames[i] = new MyStruct_Name();
        foo.aaszNames[i].name = "";
    }
    

    Good luck!

    0 讨论(0)
  • 2021-01-13 17:33
    char aaszNames[6][25];
    

    char of C++ Type is 8 bits~

    but char of C# Type is Unicode ,(16 bits) !

    so char of C++ Type <-> byte of C# type

    0 讨论(0)
  • 2021-01-13 17:42

    I would write a small c-program to check the byte size of the C-structure.
    Then I would go with the other suggestion to extract the data field by field.
    From a C standpoint the /0 is treated as normal character included in the 6 bytes whereas C# would use a length of 5 and have the /0 hidden.

    0 讨论(0)
  • 2021-01-13 17:46
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=150)]
     public char[] aaszNames;
    

    That marshalling type looks well. Probably issue in function call, or bad memory allocation/

    0 讨论(0)
提交回复
热议问题