Results 1 to 9 of 9

Thread: Structure to Byte array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    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:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim a As test
    7.         Dim b() As Byte
    8.  
    9.  
    10.         a.a = "testing"
    11.  
    12.         ReDim b(Marshal.SizeOf(a))
    13.  
    14.         Dim ptr As GCHandle = GCHandle.Alloc(b, GCHandleType.Pinned)
    15.  
    16.         Marshal.StructureToPtr(a, ptr.AddrOfPinnedObject, True)
    17.     End Sub
    18. End Class
    19.  
    20. <StructLayout(LayoutKind.Sequential)> _
    21. Public Structure test
    22.     Public a As String
    23. 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?

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Structure to Byte array

    Here is a link to some stuff in regards to Marshaling
    Marshaling Structures

  3. #3
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    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.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    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.

  6. #6
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    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...

    Signatures suck

  7. #7
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: Structure to Byte array

    Quote 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.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    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...

  9. #9
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    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...

    Signatures suck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width