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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification