Structure with MarshalAs array size

T

ThomasR

Hi together,

I have following little problem with a structure which I need for unmanaged
code:

Public Structure Info
Public Header As HeaderInfo

<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Notes As Integer()
End Structure

The above structure will fix the Array Size for Notes to 256 elements.
But I need this Info structure with a variable Notes array of 4, 8 or 256
elements.

Is there a way how this could be done in code to have a dynamic Notes size ?

I could define 3 separat Info structures but this would result in e.g.
Info4, Info8 and Info256 definitions which I can't use (makes my code to
complicated).

I would like to keep the Info structure as shown above but later I want to
define the nessesary array size.

One last question:
Do I need to create such a structure with NEW ? (e.g. Dim Test as New Info)

Cheers
Thomas

hint:
Unfortunately I posted this question the first time to the vb.com queue, but
I think this queue is better for VB.NET questions. Please forgive me for this
double posting
 
M

Mattias Sjögren

Public Structure Info
Public Header As HeaderInfo

<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Notes As Integer()
End Structure

The above structure will fix the Array Size for Notes to 256 elements.
But I need this Info structure with a variable Notes array of 4, 8 or 256
elements.
Is there a way how this could be done in code to have a dynamic Notes size ?

Not really, no. But if that's actually what the structure looks like,
can't you work with a simple Integer() instead and skip the wrapping
structure completely?

One last question:
Do I need to create such a structure with NEW ? (e.g. Dim Test as New Info)

No


Mattias
 
T

ThomasR

Hi Mattias,

no, the structure can't be removed or skipped.

I tried also a different approach, which looks like:

Public Structure Info
Public Header As HeaderInfo

<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray)> _
Public Notes As Integer()

Public Sub Initialize(ByVal i As Integer)
ReDim Notes(i)
End Sub
End Structure

I removed the size constrain from the array and added a Initialize sub to
the structure.
This will allow me to redimension the Notes array in my code. So far so
good, but the
com part which will access this structure will not work correct with it.

There seems to be some memory difference between my Initialized Notes array
and the
predefined SizeConst:=256 array size.

But I don't know what is the difference and how this can be solved from the
..NET part.

The COM part seems to use the Notes array in a unsorted order.

Cheers
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top