Memory Mapping for VB .NET structures and arrays

jlhuber

New member
Joined
Feb 22, 2004
Messages
1
I need to use an existing DLL from VB .NET. One of the arguments is a by reference structure. This works fine from VB 6 where the structures memory is contiguously mapped. In VB .NET the "dispersed" managed mapping leads to problems once the DLL function executes.

HOW can I map? Ive tried structure layoutkind.sequential and .explicit, both with fieldoffsets but have not had luck. VB .NET managed memory puts the data where it want to put it --- the offsets are about 12 bytes apart. Its curious.

Example:
<StructLayout(LayoutKind.Sequential)> _
Public Structure I1
Public n1 As Short this ends up at an address, say x
Public n2 As Short this at x+12
<MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.AsAny)> _
Public a1() As Byte this at x+24
Public Sub Initialize()
ReDim a1(4)
End Sub
End Structure

I just need to know how I can define the above structure so that:

n1 is at memory address x
n2 is at memory address x+2 since n1 is short = 2 bytes
a1(0) is at memory address x+4 since n2 is 2 bytes
a1(1) is at memory address x+5 since a1(0) is 1 bytes

Thanks for any help
JLH
 
Back
Top