Original Post
I'm converting my development tools from VB6 to VB.NET. However, I have ran into some problems regarding saving/loading the data. The majority of my tools alter data files, which are encrypted before they are saved. These tools are things such as an item editor, object editor, NPC editor, etc. To load the data with VB6, I would open the file with CreateFile() and read the contents into a byte array using ReadFile(). The byte array would then be fed into the decryption function, which would spit out a new, decrypted, byte array. I then used CopyMemory() (RtlMoveMemory) to copy the byte array into the data array, which is an array of structures. This data array could then be altered with the editor and saved to the data file again, pretty much reversing the steps to load the data. Unfortunately, VB.NET does not appear to like the CopyMemory() function, most likely because it wants to manage the memory itself. I've tried various methods to get the same functionality, but nothing has worked so far. I am currently trying to use the Serialize()/Deserialize() functions to convert to a byte array, and from a byte array to the structure array. Here is the test code. Please note that I am using "Object" instead of the actual structure array. I did this to prevent possible errors with deserializing. The code does not work even if I use the correct structure array. The following error occurs at "res = formatter.Deserialize(stream)":
Dim byBuf() As Byte
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim stream As New System.IO.MemoryStream()
Dim res As Object
' byBuf is loaded from the data file here. File IO code not included.
' Alternate way of filling the stream.
' Call formatter.Serialize(stream, byBuf)
' Fill the stream with the byte array.
stream.Write(byBuf, 0, byBuf.Length)
' Go back to the beginning of the stream. This fixed an error in a previous test.
Call stream.Seek(0, IO.SeekOrigin.Begin)
' Deserialize the stream into the object. res is used as a debugging stand-in.
res = formatter.Deserialize(stream)
Quote:I am new to VB.NET, so if there is a better way to do what I need it to do, please let me know. Help would really be appreciated. Development is stalled until these tools can be converted. Note: I no longer have access to VB6, so going back is not an option at the moment.
A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll [5] Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.