[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.remoting

Serialize / Deserialization troubles .. Please help !!

agarwalpiyushk

7/16/2004 7:12:00 PM

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
1 Answer

Cedric B

9/5/2004 3:04:00 AM

0

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" <agarwalpiyushk@yahoo.co.in> wrote in message
news:795b682f.0407161112.148c57de@posting.google.com...
> 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