TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > VB.NET

Notices

Structure to Byte Array in VB.NET

VB.NET


Reply
 
Thread Tools Display Modes
Old 14-12-2007, 11:24 PM   #1
tash.robinson@gmail.com
Guest
 
Posts: n/a
Structure to Byte Array in VB.NET

I have been pulling my hair out with this and have searched and found
some solutions but I can't seem to get them to work as expected.

I have a structure, which I have declared explicitly using
<StructLayout(LayoutKind.Explicit)>

The structure does contain some arrays, but it is something like this

<StructLayout(LayoutKind.Explicit)>
Public Structure TheStruct
<FieldOffset(0)> Dim UpdateMode As Int16
<FieldOffset(2)> Dim TimeFrameCounter As Int16
<FieldOffset(4), VBFixedArray(7)> Dim AxisCommandedPosition() As
Integer
<FieldOffset(36), VBFixedArray(7)> Dim AxisEncoderPosition() As
Integer
<FieldOffset(68), VBFixedArray(7)> Dim AxisCommandedVelocity() As
Integer
<FieldOffset(100), VBFixedArray(7)> Dim AxisStatus() As Integer
<FieldOffset(132)> Dim SystemStatus As Integer
End Structure

I need to use this as a byte array so I can send it over a ethernet
socket (after manipulating it a bit)

I have tried this:
Dim myStruct as TheStruct
Dim fsdata() As Byte


ReDim fsdata(Marshal.SizeOf(myStruct) - 1)
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(myStruct))

'get a pointer to myStruct
Marshal.StructureToPtr(myStruct, ptr, True)

'copy myStruct from the heap pointer into fsdata array
Marshal.Copy(ptr, fsdata, 0, Marshal.SizeOf(myStruct))

'release the heap memory
Marshal.FreeHGlobal(ptr)

This sometimes works, but the line
Marshal.StructureToPtr(myStruct, ptr, True)
often crashes with a message "Attempted to read or write protected
memory."
If I change it to:
Marshal.StructureToPtr(myStruct, ptr, False)
It doesnt crash, but the data that ends up in the fsdata data is
garbage - not myStruct.

Is there a way to do this in vb.net that actually works?

If I have a managed structure of a fixed size explicitly laid out, why
do I have to marshal it through unmanaged memory to use it as a byte
array?

When I call Marshal.AllocHGlobal to get a pointer why would it give me
a pointer to a protected memory area I can't use? Or is that what is
happening?

For now I have given up on using the structure and will just use a
byte array. I will write some other functions to access the peices of
the array as if it were a structure, but this is messy.

Is there a better way?

thanks.
  Reply With Quote
Old 14-12-2007, 11:27 PM   #2
Mattias Sjögren
Guest
 
Posts: n/a
Re: Structure to Byte Array in VB.NET

><StructLayout(LayoutKind.Explicit)>
>Public Structure TheStruct
> <FieldOffset(0)> Dim UpdateMode As Int16
> <FieldOffset(2)> Dim TimeFrameCounter As Int16
> <FieldOffset(4), VBFixedArray(7)> Dim AxisCommandedPosition() As
>Integer
> <FieldOffset(36), VBFixedArray(7)> Dim AxisEncoderPosition() As
>Integer
> <FieldOffset(68), VBFixedArray(7)> Dim AxisCommandedVelocity() As
>Integer
> <FieldOffset(100), VBFixedArray(7)> Dim AxisStatus() As Integer
> <FieldOffset(132)> Dim SystemStatus As Integer
>End Structure


The CLR marshaler doesn't understand the VBFixedArray attribute. Try
adding <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)>.


>If I have a managed structure of a fixed size explicitly laid out, why
>do I have to marshal it through unmanaged memory to use it as a byte
>array?


Because in the strongly typed world of .NET, you can't treat an object
of a certain type (the structure) as an entirely different type (the
byte array) even if they happen to share the same memory layout.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
|
Please reply only to the newsgroup.
  Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 04:24 AM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional