|
-
Jul 17th, 2007, 02:44 AM
#1
Thread Starter
Fanatic Member
Structure to Byte array
I'm looking into the possibility of persisting data by putting it into a structure and then copying the structure (via the marshal class) to a byte array which is then output to the file.
I'm using the following code:
VB Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As test
Dim b() As Byte
a.a = "testing"
ReDim b(Marshal.SizeOf(a))
Dim ptr As GCHandle = GCHandle.Alloc(b, GCHandleType.Pinned)
Marshal.StructureToPtr(a, ptr.AddrOfPinnedObject, True)
End Sub
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Structure test
Public a As String
End Structure
What I'm getting, however, is b()'s length is set to 5 and only the first three elements are use and contain different values each time. Why isn't the actual contents of the structure contained here? Is there a correct way of doing this? Am I not understanding what GCHandle.Alloc and Marshal.StructureToPtr do?
-
Jul 17th, 2007, 03:27 AM
#2
Re: Structure to Byte array
Here is a link to some stuff in regards to Marshaling 
Marshaling Structures
-
Jul 17th, 2007, 08:36 AM
#3
Re: Structure to Byte array
If you're wanting to go to a file and nothing else other than your program needs to read the resulting file, why don't you use binary serialization? It would be a lot easier to implement.
-
Jul 17th, 2007, 09:06 AM
#4
Re: Structure to Byte array
I agree with bgmacaw, binary serialization takes about two lines to implement, and you need to add the <Serializable()> attribute to the structure definition.
My usual boring signature: Nothing
 
-
Jul 18th, 2007, 01:31 AM
#5
Thread Starter
Fanatic Member
Re: Structure to Byte array
I'm trying to serialize about 3-16 mb worth of byte arrays. I figured it would be faster to copy it from memory to the harddrive rather than 1) use reflection to list all the public/private fields 2) use reflection to ascertain the values of the fields 3) watch for duplicate references and circular references and 4) dump the whole thing to a file.
I've tried binary serialization. Too slow and/or "OutOfMemoryException".
I'd rather create a byte array at the same location in memory as the structure and read it to an IO.Stream.
Yes, I'm aware that you can only do this with value types (i.e. structures). I'm not using any classes for the data I'm saving... only structures.
-
Jul 18th, 2007, 03:36 AM
#6
Hyperactive Member
Re: Structure to Byte array
I have used serialization to serialize and deserialize a 2MB - 25MB file and it was fast enought to not notice any lag ect when working with the smaller files and only a verry small amount of lag when dealing with the larger files. Also you shouldnt get an out of memory exception when working with files of that size, are you sure you closed the streams etc...
-
Jul 18th, 2007, 08:03 AM
#7
Re: Structure to Byte array
 Originally Posted by agent
I've tried binary serialization. Too slow and/or "OutOfMemoryException".
I agree that it sounds like something didn't get released properly. I've done serialization for web services on files up to 150mb without any hitches.
-
Jul 20th, 2007, 01:24 AM
#8
Thread Starter
Fanatic Member
Re: Structure to Byte array
I'd also be interested in using a progress bar without having to deal with ISerializable.
A couple of the data I'm working with are stored as byte arrays already. Maybe I'll write an IBytes interface to implement a Bytes() as Byte property and just switch to classes that implement this...
-
Jul 20th, 2007, 05:42 PM
#9
Hyperactive Member
Re: Structure to Byte array
Thats A good Idea, I usually have a SaveToFile method in my serializable classes, but your Idea of returning the bytes is a much better Idea, as then the SaveToFile can just call the getBytes method and save them to file, makes for a bit more functionality I think, Thanks for the idea...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|