473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

serialization shared memory problem

Hi,

I'm serializing a class and using shared memory / deserialization to
send it to other processes.

I can serialize with one app and deserialize with another instance of
the same app.

But if I try to deserialize with another different app, I get an
exception (the assembly RegionViewer is the app that serialized the
data):

<Exception>
An unhandled exception of type
'System.Runtime.Serialization.SerializationExcepti on' occurred in
mscorlib.dll

Additional information: Cannot find the assembly RegionViewer,
Version=1.0.2466.11715, Culture=neutral, PublicKeyToken=null.
</Exception>

Is this a security thing with serializing? Is there some way of getting
around it?

Thanks,
Norvin

Oct 2 '06 #1
9 2048
Do you need to set typeFilterLevel=Full ?

Just a guess really, more info here...
http://msdn2.microsoft.com/en-us/library/5dxse167.aspx

no*****@yahoo.com wrote:
Hi,

I'm serializing a class and using shared memory / deserialization to
send it to other processes.

I can serialize with one app and deserialize with another instance of
the same app.

But if I try to deserialize with another different app, I get an
exception (the assembly RegionViewer is the app that serialized the
data):

<Exception>
An unhandled exception of type
'System.Runtime.Serialization.SerializationExcepti on' occurred in
mscorlib.dll

Additional information: Cannot find the assembly RegionViewer,
Version=1.0.2466.11715, Culture=neutral, PublicKeyToken=null.
</Exception>

Is this a security thing with serializing? Is there some way of getting
around it?

Thanks,
Norvin
Oct 2 '06 #2
Hi Chris ,

Chris Fulstow wrote:
Do you need to set typeFilterLevel=Full ?

Just a guess really, more info here...
http://msdn2.microsoft.com/en-us/library/5dxse167.aspx
Wow, that filterTypeLevel looks like a nightmare. Since I'm not
remoting, and the class I want to copy is fairly simple, perhaps a step
back is in order. Maybe I don't need to be serializing here.

A question: I have a class, I have a destination pointer, how can I do
the equivalent of memcpy( ptrDestination, &myClass, sizeof(myClass); ?

It looks like all the overloads for Marshal.Copy require some kind of
byte array representation of the data.

Below is my class definition:

public class Sync
{
public string sensorType;
public string imageType;
public string imageNumber;
public string imageChannel;
public string imageFace;
public string extension;
public Point vScroll;
public int overlayAlpha;
public decimal xScale;
public decimal yScale;
public FLAGS signal;
public string currentFolder;
public Rectangle highlightRect;
}

Oct 2 '06 #3

<no*****@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
| Hi Chris ,
|
| Chris Fulstow wrote:
| Do you need to set typeFilterLevel=Full ?
| >
| Just a guess really, more info here...
| http://msdn2.microsoft.com/en-us/library/5dxse167.aspx
| >
|
| Wow, that filterTypeLevel looks like a nightmare. Since I'm not
| remoting, and the class I want to copy is fairly simple, perhaps a step
| back is in order. Maybe I don't need to be serializing here.
|
| A question: I have a class, I have a destination pointer, how can I do
| the equivalent of memcpy( ptrDestination, &myClass, sizeof(myClass); ?
|
| It looks like all the overloads for Marshal.Copy require some kind of
| byte array representation of the data.
|
| Below is my class definition:
|
| public class Sync
| {
| public string sensorType;
| public string imageType;
| public string imageNumber;
| public string imageChannel;
| public string imageFace;
| public string extension;
| public Point vScroll;
| public int overlayAlpha;
| public decimal xScale;
| public decimal yScale;
| public FLAGS signal;
| public string currentFolder;
| public Rectangle highlightRect;
| }
|
You could use a BinaryFormater to serialize the object to a MemoryStream.
Following is a small snip that illustrates how one could go for the
serializer, the deserializer should perform the inverse operations.
Sync mySync = new Sync();
....
IntPtr pBuffer = MapViewOfFile(....);

MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, mySync);
// Write the mapped portion length at the start of the shared memory
buffer.
// The deserializer must pick up this size from the shared portion to
reconstruct the object.
Marshal.WriteInt32(new IntPtr(pBuffer), (int)ms.Length);
byte[] mngdBuffer = ms.GetBuffer();
Marshal.Copy(mngdBuffer, 0, new IntPtr((int)pBuffer + 4),
(int)ms.Length);
UnmapViewOfFile(pBuffer);

Willy.


Oct 2 '06 #4
Hi Willy,

Willy Denoyette [MVP] wrote:
>
<no*****@yahoo.comwrote in message
| A question: I have a class, I have a destination pointer, how can I do
| the equivalent of memcpy( ptrDestination, &myClass, sizeof(myClass); ?
You could use a BinaryFormater to serialize the object to a MemoryStream.
Following is a small snip that illustrates how one could go for the
serializer, the deserializer should perform the inverse operations.
What you suggest is exactly what I'm doing at the moment. As my initial
post in this thread indicates, I have security issues when I try to
deserialize a stream that was serialized in another app.

So:
- either there is a simple way to get around those security exceptions.
After all, I have control of the code for both the app doing the
serializing as well as the app doing the deserializing.
- OR there must be a simple way to copy a class to memory without first
having to serialize it.

Any suggestions to accomplish either of the above?

Norvin

Oct 4 '06 #5
I did something similar almost 2 years ago, so I don't remember the details,
but I had the same problem that you are having. I was serializing an object
and sending it over the network and then deserialized it and was getting an
error. After searching on google it turned out that when an app serializes
an object it puts some identifying information in a property that the
deserializing app will not have which is what caused the error. As I
remember it, I had to set this property to nothing, and that took care of
the problem.

<no*****@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Hi Willy,

Willy Denoyette [MVP] wrote:
>>
<no*****@yahoo.comwrote in message
| A question: I have a class, I have a destination pointer, how can I do
| the equivalent of memcpy( ptrDestination, &myClass, sizeof(myClass); ?
You could use a BinaryFormater to serialize the object to a MemoryStream.
Following is a small snip that illustrates how one could go for the
serializer, the deserializer should perform the inverse operations.

What you suggest is exactly what I'm doing at the moment. As my initial
post in this thread indicates, I have security issues when I try to
deserialize a stream that was serialized in another app.

So:
- either there is a simple way to get around those security exceptions.
After all, I have control of the code for both the app doing the
serializing as well as the app doing the deserializing.
- OR there must be a simple way to copy a class to memory without first
having to serialize it.

Any suggestions to accomplish either of the above?

Norvin

Oct 5 '06 #6

<no*****@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| Hi Willy,
|
| Willy Denoyette [MVP] wrote:
| >
| <no*****@yahoo.comwrote in message
| | A question: I have a class, I have a destination pointer, how can I do
| | the equivalent of memcpy( ptrDestination, &myClass, sizeof(myClass); ?
| >
| >
| You could use a BinaryFormater to serialize the object to a
MemoryStream.
| Following is a small snip that illustrates how one could go for the
| serializer, the deserializer should perform the inverse operations.
|
| What you suggest is exactly what I'm doing at the moment. As my initial
| post in this thread indicates, I have security issues when I try to
| deserialize a stream that was serialized in another app.
|
| So:
| - either there is a simple way to get around those security exceptions.
| After all, I have control of the code for both the app doing the
| serializing as well as the app doing the deserializing.
| - OR there must be a simple way to copy a class to memory without first
| having to serialize it.
|
| Any suggestions to accomplish either of the above?
|
| Norvin
|

I don't see where the security exception comes from.

This should work, supposed at the serializer side you have something like
this:

Sync mySync= new Sync();
mySync.sensorType = "....";
// other fields
MemoryStream ms = new MemoryStream();
IntPtr pBuf = // pointer returned from the MapViewOfFile API call
if (pBuf != IntPtr.Zero)
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, mySync);
Marshal.WriteInt32(pBuf, (int)ms.Length);
byte[] buff = ms.GetBuffer();
Marshal.Copy(buff, 0, new IntPtr((int)pBuf + 4), (int)ms.Length);
}
...

and at the deserializer side, this:
..
int len = 0;
len = Marshal.ReadInt32(pBuf, 0);
byte[] data = new byte[len];
Marshal.Copy(new IntPtr((int)pBuf + sizeof(int)), data, 0, len);
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
Sync s = (Sync) bf.Deserialize(ms);
Console.WriteLine("{0} {1} {2} {3}", s.sensorType, s.vScroll,
s.overlayAlpha, s.xScale);

and both use the same type "Sync" from a shared assembly.

Willy.

Oct 5 '06 #7
Hi Gino,

This sounds like the same problem I'm seeing. The exception that I'm
getting complains that it can't find info about the app that did the
serializing:

"Cannot find the assembly RegionViewer,
Version=1.0.2466.11715, Culture=neutral, PublicKeyToken=null."

where RegionViewer is the app that did the serializing. Do you happen
to remember what object the property was in? The formatter, or the
stream itself?

Thanks,
Norvin

Gino wrote:
I did something similar almost 2 years ago, so I don't remember the details,
but I had the same problem that you are having. I was serializing an object
and sending it over the network and then deserialized it and was getting an
error. After searching on google it turned out that when an app serializes
an object it puts some identifying information in a property that the
deserializing app will not have which is what caused the error. As I
remember it, I had to set this property to nothing, and that took care of
the problem.

<no*****@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Hi Willy,

Willy Denoyette [MVP] wrote:
>
<no*****@yahoo.comwrote in message
| A question: I have a class, I have a destination pointer, how can I do
| the equivalent of memcpy( ptrDestination, &myClass, sizeof(myClass); ?
You could use a BinaryFormater to serialize the object to a MemoryStream.
Following is a small snip that illustrates how one could go for the
serializer, the deserializer should perform the inverse operations.
What you suggest is exactly what I'm doing at the moment. As my initial
post in this thread indicates, I have security issues when I try to
deserialize a stream that was serialized in another app.

So:
- either there is a simple way to get around those security exceptions.
After all, I have control of the code for both the app doing the
serializing as well as the app doing the deserializing.
- OR there must be a simple way to copy a class to memory without first
having to serialize it.

Any suggestions to accomplish either of the above?

Norvin
Oct 5 '06 #8
If anyone is interested, I ended up solving this problem.

If you serialize in one app, and want to deserialize in another app, an
exception will be thrown if dotnet can't load the assembly that did the
serializing.

Option 1: register the serializing app in the GAC. I didn't try this,
but I assume it would work
Option 2: use the SerializationBinder class to effectively fool the
deserializing app into thinking that it did the serializing:
when setting your BinaryFormatter up to deserialize, set the Binder
property to the class below:

BinaryFormatter b = new BinaryFormatter();
b.Binder = new FooDeserializationBinder();
myClasss = (MyClass) b.Deserialize(stream);

sealed class FooDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string
typeName)
{
return Type.GetType(typeName + ", " +
Assembly.GetExecutingAssembly().FullName);
}
}

no*****@yahoo.com wrote:
Hi Gino,

This sounds like the same problem I'm seeing. The exception that I'm
getting complains that it can't find info about the app that did the
serializing:

"Cannot find the assembly RegionViewer,
Version=1.0.2466.11715, Culture=neutral, PublicKeyToken=null."

where RegionViewer is the app that did the serializing. Do you happen
to remember what object the property was in? The formatter, or the
stream itself?

Thanks,
Norvin

Gino wrote:
I did something similar almost 2 years ago, so I don't remember the details,
but I had the same problem that you are having. I was serializing an object
and sending it over the network and then deserialized it and was getting an
error. After searching on google it turned out that when an app serializes
an object it puts some identifying information in a property that the
deserializing app will not have which is what caused the error. As I
remember it, I had to set this property to nothing, and that took care of
the problem.

<no*****@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Hi Willy,
>
Willy Denoyette [MVP] wrote:
>>
><no*****@yahoo.comwrote in message
>| A question: I have a class, I have a destination pointer, how can I do
>| the equivalent of memcpy( ptrDestination, &myClass, sizeof(myClass); ?
>>
>>
>You could use a BinaryFormater to serialize the object to a MemoryStream.
>Following is a small snip that illustrates how one could go for the
>serializer, the deserializer should perform the inverse operations.
>
What you suggest is exactly what I'm doing at the moment. As my initial
post in this thread indicates, I have security issues when I try to
deserialize a stream that was serialized in another app.
>
So:
- either there is a simple way to get around those security exceptions.
After all, I have control of the code for both the app doing the
serializing as well as the app doing the deserializing.
- OR there must be a simple way to copy a class to memory without first
having to serialize it.
>
Any suggestions to accomplish either of the above?
>
Norvin
>
Oct 7 '06 #9
Weird, you shouldn't get a "security" exception when a type failed to load,
you should receive a typeload exception.
Also you don't need the type that did the serialization, what you need is
the type you are serializing/deserializing. This type can be installed as a
(preferably) private or as a global assembly, depending your needs. The
SerializationBinder is an option, but IMO shouldn't be used in this case,
it's an option when you need to deserialize to a different version.

Willy.
<no*****@yahoo.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
| If anyone is interested, I ended up solving this problem.
|
| If you serialize in one app, and want to deserialize in another app, an
| exception will be thrown if dotnet can't load the assembly that did the
| serializing.
|
| Option 1: register the serializing app in the GAC. I didn't try this,
| but I assume it would work
| Option 2: use the SerializationBinder class to effectively fool the
| deserializing app into thinking that it did the serializing:
|
|
| when setting your BinaryFormatter up to deserialize, set the Binder
| property to the class below:
|
| BinaryFormatter b = new BinaryFormatter();
| b.Binder = new FooDeserializationBinder();
| myClasss = (MyClass) b.Deserialize(stream);
|
| sealed class FooDeserializationBinder : SerializationBinder
| {
| public override Type BindToType(string assemblyName, string
| typeName)
| {
| return Type.GetType(typeName + ", " +
| Assembly.GetExecutingAssembly().FullName);
| }
| }
|
| no*****@yahoo.com wrote:
| Hi Gino,
| >
| This sounds like the same problem I'm seeing. The exception that I'm
| getting complains that it can't find info about the app that did the
| serializing:
| >
| "Cannot find the assembly RegionViewer,
| Version=1.0.2466.11715, Culture=neutral, PublicKeyToken=null."
| >
| where RegionViewer is the app that did the serializing. Do you happen
| to remember what object the property was in? The formatter, or the
| stream itself?
| >
| Thanks,
| Norvin
| >
| >
| >
| Gino wrote:
| I did something similar almost 2 years ago, so I don't remember the
details,
| but I had the same problem that you are having. I was serializing an
object
| and sending it over the network and then deserialized it and was
getting an
| error. After searching on google it turned out that when an app
serializes
| an object it puts some identifying information in a property that the
| deserializing app will not have which is what caused the error. As I
| remember it, I had to set this property to nothing, and that took care
of
| the problem.
|
|
|
| <no*****@yahoo.comwrote in message
| news:11**********************@k70g2000cwa.googlegr oups.com...
| Hi Willy,
| >
| Willy Denoyette [MVP] wrote:
| >>
| ><no*****@yahoo.comwrote in message
| >| A question: I have a class, I have a destination pointer, how can
I do
| >| the equivalent of memcpy( ptrDestination, &myClass,
sizeof(myClass); ?
| >>
| >>
| >You could use a BinaryFormater to serialize the object to a
MemoryStream.
| >Following is a small snip that illustrates how one could go for the
| >serializer, the deserializer should perform the inverse operations.
| >
| What you suggest is exactly what I'm doing at the moment. As my
initial
| post in this thread indicates, I have security issues when I try to
| deserialize a stream that was serialized in another app.
| >
| So:
| - either there is a simple way to get around those security
exceptions.
| After all, I have control of the code for both the app doing the
| serializing as well as the app doing the deserializing.
| - OR there must be a simple way to copy a class to memory without
first
| having to serialize it.
| >
| Any suggestions to accomplish either of the above?
| >
| Norvin
| >
|
Oct 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Google Mike | last post by:
I'm using PHP 4.2.2 on RH9 Linux (with Progeny.com updates, in case you're wondering). I was using shared memory (shm* API) a great deal in my web applications because I have 1GB of RAM on this...
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
11
by: Michael Schuler | last post by:
The use of STL in shared memory poses a real problem since (non-smart) pointers are not allowed there. Is there any solution for containers in shared memory using smart pointers? Where can I...
2
by: andreas | last post by:
Hi, if have a object arrSdList of type SortedList for serialization and deserialization there are two subs for doing this Public Sub deser Dim Formatter As BinaryFormatter = New...
10
by: Michael Maes | last post by:
Hi, I have a BaseClass from which many Classes Derive. In short: the BaseClass provides the functionalities (Methods) and the Derived Classes extend it with Properties. One of the (Base)...
1
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
2
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for...
3
by: Marc | last post by:
Hi, I am trying to serialize a data structure -- a list (of custom class) -- in one application, then read it in with another application. My serialize and deserialize subs are in a module that...
8
by: =?Utf-8?B?UGlnZ3k=?= | last post by:
Hi to all, I am getting this System.OutOfMemoryException calling the Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(<stream>,<Obj>) method. The type of <streamis...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.