Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Structure Sizes for Interoperability Not Adding Up 1

Status
Not open for further replies.

bustell

Programmer
Mar 22, 2002
159
US
I am having trouble creating a structure that I can send to a Delphi 5 DLL. The DLL is expecting a structure containing 9 bytes containing 1 Byte and 2 Int32's.

If I create a structure with just the one byte field, the Marshal.SizeOf() equals 1.

If I create a structure with the two Int32 fields, the Marshal.SizeOf() equals 8.

However, if I create the structure with all three fields, the Marshal.SizeOf() equals 12. This does not add up, it should be equal to 9.

I have tried using a structure attribute of <StructLayout(layoutkind.Sequential)> and it did not help.

I tried using a structure attribute of <StructLayout(layoutkind.Explicit)> with <FieldOffset(n)> attributes on the fields and it did not help.

I really need the structure to be 9 bytes. I hope someone has run into this and can help.

Here's the Code:
Code:
Private Sub btn1_Click(ByVal sAs System.Object, ByVal e As System.EventArgs) Handles btn1.Click

  Dim myTest1 as TTest1
  Dim myTest2 as TTest2
  Dim iSize1, iSize2 as Int32
  iSize1 = Marshal.SizeOf(myTest1)
  iSize2 = Marshal.SizeOf(myTest2)
	
End Sub
	
Public Structure TTest1             '          Size
  Public ActiveMessage As Int32     'PChar     004
  Public ActiveMessageSize As Int32 'Integer   004
End Structure
	
Public Structure TTest2             '          Size
  Public Active As Byte             'Boolean   001
  Public ActiveMessage As Int32     'PChar     004
  Public ActiveMessageSize As Int32 'Integer   004
End Structure


Pat B
 
Did you try LayoutKind.Sequential with the Pack option of 0?

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip,

You are my hero! Though setting Pack to 0 did not fix it. Setting it to 1 did fix it. Zero sets it to the OS default.

Thanks so much for pointing me in the right direction.




Pat B
 
Ooops.
But glad you got it to work.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top