Serialize / Deserialization troubles .. Please help !
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.remoting.

Post a new message to this list...

piyush
Hello,
I am trying to use shared memory mapped files for IPC between two
applications, one of which may be in C++ (non .NET envi)

As a first step I am trying to get serialization / deserializtion
working within a single application and getting exceptions and errors.

"Binary stream does not contain a valid Binary header, 0 possible
causes, invalid stream or object version change between serialization
and deserialization"

The code is (CURRENTLY ITS IN A SINGLE APPLICATION ) :

[StructLayout (LayoutKind.Sequential)]
[Serializable]
public struct MYREC
{
    public string blah;
    public string foo;
}

MYREC myRecord = new MYREC();
myRecord.blah = "blah";
myRecord.foo = "foo";

MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();

bf.Serailize(ms, myRecord);
byte[] buffer = ms.GetBuffer();
int size = Marshal.Sizeof(myRecord);
for(int i=0;i < size; ++i)
{
Marshal.WriteByte( (IntPtr) (mp.ToInt64() + i), buffer[i]);
}

//WHAT EXACTLY SHOULD BE IN SECOND APPLICATION, BUT IS IN SAME APP NOW

MYREC display = new MYREC();
MessageBox.Show ("Trying ot get the structure back here:");
byte[] rec = new byte[size];
for(int j=0 ; j < size ; j++)
{
rec[j] = Marshal.ReadByte ( (IntPtr) (mp.ToInt64() + j));
}

BinaryFormatter newbf = new BinaryFormatter();
MemoryStream newms = new MemoryStream (rec);
newms.Position = 0; //dont know if this should be there
display = (MYREC) newbf.Deserialization(newms);

Any clues are greatly appreciated .. and its urgent please !

Thanks !
Piyush
Reply to this message...
 
    
Cedric B
I just ran across this issue in a C# application that I am writing. I fixed
it by telling the memory stream to use ASCII encoding before deserializing
the stream:

BinaryFormatter tmpFormatter = new BinaryFormatter();

tmpFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;

MemoryStream readStream = new
MemoryStream(Encoding.ASCII.GetBytes(predicateEntity.PredicateData));

PredicateData savedData = (PredicateData)formatter.Deserialize(readStream);

I dont know if this will help you or not.

"piyush" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.IntPtr
System.IO.MemoryStream
System.Runtime.InteropServices.LayoutKind
System.Runtime.InteropServices.Marshal
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
System.Runtime.Serialization.Formatters.FormatterAssemblyStyle
System.Text.Encoding
System.Windows.Forms.MessageBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com