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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TTheot
    New Member
    • Feb 2010
    • 6

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

    Hallo everyone.

    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 MyStruct
    {
    int siOrder;
    char aaszNames[6][25];
    int siId[6];
    int siTones[6];
    };

    I've defined the managed structure as following:

    <StructLayoutAt tribute(LayoutK ind.Sequential, CharSet:=CharSe t.[Ansi])> _
    Public Structure MyStruct
    Public siOrder As Integer

    <MarshalAsAttri bute(UnmanagedT ype.ByValTStr, SizeConst:=150) > _
    Public aaszNames As String

    <MarshalAsAttri bute(UnmanagedT ype.ByValArray, SizeConst:=6, ArraySubType:=U nmanagedType.I4 )> _
    Public siId() As Integer

    <MarshalAsAttri bute(UnmanagedT ype.ByValArray, SizeConst:=6, ArraySubType:=U nmanagedType.I4 )> _
    Public siTones() As Integer
    End Structure

    The C++ dll writes to a log file when a function is called. From this log I can see that the structure cannot be resolved.

    My issues are the following:

    a) Marshal.SizeOf( MyStruct) is computed as 204, but as I can see, the size seems to be: 4 + 150 + 6*4 + 6*4 = 202 bytes. What are these two extra bytes, and where are they supposed to be padded?

    b) How should I populate the aaszNames field? The C++ expects 6 rows of 25 character long strings, but my individual values are of less length (eg "John", "Robert", etc). How should I concatenate the string?

    Could anyone give me a hint about these issues?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    a) I suspect you are getting 2 bytes of padding after the array aaszNames so that siId appears on a 4 byte boundary. Note that 150 + 4 = 154 is not a 4 byte boundary. A 32 bit machine is going to prefer having its 32bit values on 4 byte (32 bit) boundaries.

    b) Since aaszNames looks like it is supposed to be an array of C style zero terminated strings you should copy your strings into the array and write a zero terminate (character value 0) at the end of every string. If you get a string > 24 characters you will need to truncate it to 24 characters to leave room for the zero terminator.

    Comment

    • TTheot
      New Member
      • Feb 2010
      • 6

      #3
      Thnx for answering Banfa.
      a)Yes, these two bytes make sense to me now! So, I suppose I should not alter anything in my declaration, and leave .NET do the trick, correct?

      b) Do you suggest I should declare aaszNames as one dimensional char array with 150 length, including a null terminator after each string? Although I have tried that, with no success, I'll give it a try again. Maybe I did something wrong ;)

      Comment

      • TTheot
        New Member
        • Feb 2010
        • 6

        #4
        Well, I tried once more declaring aaszNames as one dimensional char array with 150 length, but again, no luck...
        I'm stuck...

        Comment

        Working...