fixed array inside a structure

SleepyTim

New member
Joined
Dec 11, 2002
Messages
2
Hi,

Im looking into VB.net for a project but I have come against a problem. Structures required in some DLLs have arrays of byte such as
[vB]
Type WSAData
A(100) As Byte
B As Integer
End Type
[/vB]

The problem is I cant find out how i can replicate this structure in VB.net since you cant use Type definition but have to use Structure, in which you can only define a reference to a dynamic array.


the problem is thus:

[vB]
Structure MyStruct
Public MyArray() As Integer
Public MyInt As Integer
End Structure

Sub UseStruct()
Dim Struct As MyStruct
ReDim Struct.MyArray(9) Dimension as 10 elements
Struct.MyArray(2) = 777 Use the array.
End Sub
[/vB]

would in fact produce something like the following behind the sceens:
Pointer MyArray (this uses up 4 bytes of data)
Integer MyInt (this uses up 4 bytes of data)

Somewhere else in memory a block of 10 bytes of memory is allocated and the variable MyArray references it


In contrast old style VB code

[vB]
Type MyStruct
MyArray(9) As Integer
MyInt As Integer
End Type
[/vB]

would produce something like the following behind the sceens:
Integer *10 MyArray (this uses up 4*10 bytes of data = 40)
Integer MyInt (this uses up 4 bytes of data)

So as you can see there is a big difference in terms of where the data is stored in memory, and the DLL im interfacing with will only accept the data in the format produced by the Type definition.


Is there any way arround this?

thanks

Tim
 
Im not 100% sure about what your problem is, but you may want
to try declaring your Integers as Int16 in your structure, if its the
number of bytes youre worried about. I believe that Integers in
VB.NET are the same as Longs in VB6, and Int16 represents the
original Integers from VB6. I could be mistaken though.
 
Thats not what i need but thanks..

The problem I have is that
Code:
Structure MyStruct
   Public MyArray() As Integer
   Public MyInt As Integer
End Structure

Sub UseStruct()
   Dim Struct As MyStruct
   ReDim Struct.MyArray(3)  Dimension as 10 elements
End Sub
Will make the following in RAM :
({MyArray - pointer to an array = 4 Bytes}{MyInt - Integer = 4 Bytes})
And somewhere else in memory allocated dynamically
({MyArray[0] - Integer = 4 Bytes}{MyArray[1] - Integer = 4 Bytes}{MyArray[2] - Integer = 4 Bytes}{MyArray[3] - Integer = 4 Bytes})

But what I need is:
({MyArray[0] - Integer = 4 Bytes}{MyArray[1] - Integer = 4 Bytes}{MyArray[2] - Integer = 4 Bytes}{MyArray[3] - Integer = 4 Bytes}{MyInt - Integer = 4 Bytes})

In this case it could be simulated by doing
Code:
Structure MyStruct
   Public MyArray0 As Integer
   Public MyArray1 As Integer
   Public MyArray2 As Integer
   Public MyArray3 As Integer
   Public MyInt As Integer
End Structure
But this is not a nice work arround for starters and also some of the structures have 250+ ellement and so would be take up a lot of space and time to define like this.

I have posted this problem on other VB message boards but no one seems to know of a solution.

If anyone knows of one then Please do let me know

thanks
Tim
 
You wont find a way to change the way VB.NET handles arrays in structures, but you WILL find a way to make it marshal the structure correctly to your DLL. At this point you may want to tell us more about your DLL and what its expecting, but you can use attributes to make sure that when your structure is passed, any arrays are passed with all their values sequentially, just like you need.

Code:
< StructLayout( LayoutKind.Sequential )> _
Public Structure MyArrayStruct 
   Public flag As Boolean
   < MarshalAs( UnmanagedType.ByValArray, SizeConst:=3 )> _
   Public vals As Integer()
End Structure MyArrayStruct

Can I suggest looking at the MSDN page under Programming .NET -> Interoperating -> Interop Marshaling -> Platform Invoke -> Structs

Or, if you have the SDK installed,

[mshelp]ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconstructssample.htm[/mshelp]
 
Last edited by a moderator:
Back
Top