M
marco_segurini
Hi,
From my VB program I call a C++ function that gets a structure pointer
like parameter. The structure has a field that contains the structure
length and other fields.
My problem is that each 'double' fields get 12 bytes instead of 8 so
the structure length results wrong.
'----Sample
Imports System.Runtime.InteropServices
Module Module1
<StructLayout(LayoutKind.Sequential)> Public Structure
structDoubleSeq
Public size As Integer
Public d As Double
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
structDoubleSeqUn
Public size As Integer
<MarshalAs(UnmanagedType.R8)> Public d As Double
End Structure
<StructLayout(LayoutKind.Explicit)> Public Structure
structDoubleExpl
<FieldOffset(0)> Public size As Integer
<FieldOffset(4)> Public d As Double
End Structure
Sub Main()
Dim strSeq As structDoubleSeq
strSeq.size = Marshal.SizeOf(GetType(structDoubleSeq))
'WRONG: the size is set to 16
Dim strSeqUn As structDoubleSeqUn
strSeqUn.size = Marshal.SizeOf(GetType(structDoubleSeqUn))
'WRONG: the size is set to 16
Dim strExpl As structDoubleExpl
strExpl.size = Marshal.SizeOf(GetType(structDoubleExpl))
'OK: this size is set to 12
End Sub
End Module
'----- End Sample
Using <StructLayout(LayoutKind.Explicit)> all work fine but I like to
work with <StructLayout(LayoutKind.Sequential)> that is less error
prone in case of structure with a lot of members.
Is there any directive to 'compact' the structure?
Is there any directive to 'compact' a single field (like double)?
TIA.
Marco.
From my VB program I call a C++ function that gets a structure pointer
like parameter. The structure has a field that contains the structure
length and other fields.
My problem is that each 'double' fields get 12 bytes instead of 8 so
the structure length results wrong.
'----Sample
Imports System.Runtime.InteropServices
Module Module1
<StructLayout(LayoutKind.Sequential)> Public Structure
structDoubleSeq
Public size As Integer
Public d As Double
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
structDoubleSeqUn
Public size As Integer
<MarshalAs(UnmanagedType.R8)> Public d As Double
End Structure
<StructLayout(LayoutKind.Explicit)> Public Structure
structDoubleExpl
<FieldOffset(0)> Public size As Integer
<FieldOffset(4)> Public d As Double
End Structure
Sub Main()
Dim strSeq As structDoubleSeq
strSeq.size = Marshal.SizeOf(GetType(structDoubleSeq))
'WRONG: the size is set to 16
Dim strSeqUn As structDoubleSeqUn
strSeqUn.size = Marshal.SizeOf(GetType(structDoubleSeqUn))
'WRONG: the size is set to 16
Dim strExpl As structDoubleExpl
strExpl.size = Marshal.SizeOf(GetType(structDoubleExpl))
'OK: this size is set to 12
End Sub
End Module
'----- End Sample
Using <StructLayout(LayoutKind.Explicit)> all work fine but I like to
work with <StructLayout(LayoutKind.Sequential)> that is less error
prone in case of structure with a lot of members.
Is there any directive to 'compact' the structure?
Is there any directive to 'compact' a single field (like double)?
TIA.
Marco.