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:

    Code:
    struct MyStruct
    {
    	int	 siOrder;
    	char	 aaszNames[6][25];
    	int     siId[6];
    	int     siTones[6];		
    };
    I've defined the managed structure as following:

    Code:
    <StructLayoutAttribute(LayoutKind.Sequential, CharSet:=CharSet.[Ansi])> _
    Public Structure MyStruct
        Public siOrder As Integer
    
        <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=150)> _
        Public aaszNames As String
    
        <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=6, ArraySubType:=UnmanagedType.I4)> _
        Public siId() As Integer
    
        <MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst:=6, ArraySubType:=UnmanagedType.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?
    Last edited by tlhintoq; Feb 12 '10, 05:19 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Is this a C++ or VB question? If it a C++ question you would get a faster reply to your question if it were posted in the C++ section and not the VB section.

    Hope you get your answer,
    CroCrew~

    Comment

    • TTheot
      New Member
      • Feb 2010
      • 6

      #3
      Well, I could also try the C++ section... ;)

      Comment

      • CroCrew
        Recognized Expert Contributor
        • Jan 2008
        • 564

        #4
        Lets see if we can get this moved to the C++ section.

        Good Luck in getting your answer,
        CroCrew~

        Comment

        • TTheot
          New Member
          • Feb 2010
          • 6

          #5
          Thanx CroCrew :)

          Hoping for some hints myself..

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Thread moved. Hope you find your answers.

              Comment

              Working...