尝试将非托管内存块转换为托管对象时的元帅异常

问题描述:

我发现我们可以使用marshal类通过套接字向对象发送对象.我可以发送对象,但是在客户端,当我接收到将其转换为托管对象的字节数组时,它会引发异常,我不知道异常的含义,因此如果有人可以告诉我它是什么,将感激不尽.这是我的代码:

I figure out that we can send a object trough a socket using the marshal class. I can send the object but at the client-side when I receive the byte array to convert it to a managed object, it throw a exception, I don''t know what the exception means, so if anyone could tell what it is i would be grateful. Here is my code:

public static object RawDeserialize(byte[] rawdatas, Type type)//this stays at the client-side
        {
            int rawsize = Marshal.SizeOf(type);
            if (rawsize > rawdatas.Length) return null;
            IntPtr buffer = Marshal.AllocHGlobal(rawsize);
            Marshal.Copy(rawdatas, 0, buffer, rawsize);
            object retobj = return Marshal.PtrToStructure(buffer, type); /* the exception occurs here and tells me "Attempted to read or write protected memory. This is usually an indication that other memory impaired." */
            Marshal.FreeHGlobal(buffer);
            return retobj;
        }



这是用于在字节数组中转换对象的服务器端代码,在这里工作正常



here are the server side code for converting a object in a byte array, here this works fine

public static byte[] SerializeExact(object anything)//this stays at the server-side
        {
            int structsize = Marshal.SizeOf(anything);
            IntPtr buffer = Marshal.AllocHGlobal(structsize);
            Marshal.StructureToPtr(anything, buffer, false);
            byte[] streamdatas = new byte[structsize];
            Marshal.Copy(buffer, streamdatas, 0, structsize);
            Marshal.FreeHGlobal(buffer);
            return streamdatas;
        }



感谢所有查看或回答的人.

PS:我已经学习了如何通过C#中的套接字发送字节数组.


好的,我已经弄清楚了为什么序列化对象,结构时会引发该异常……这是我在codeProject网站上由Marker Salsbery先生找到的答案:
附加到序列化数据的是定义序列化数据类型的程序集的名称/版本.系统尝试使用该程序集来获取将数据反序列化为相同类型的对象所需的类型信息[因此它会引发异常,例如服务器,版本= 1.0.0.0,文化=中性,公共密钥令牌= null"].您可以在两端使用相同的程序集
或在反序列化端使用SerializationBinder."

在此处查看更多信息 http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.serializationbinder.aspx [



thanks for anyone who viewed or answer.

PS: I''ve learned how to send a byte array though a socket in C#.


Ok I''ve figured out why it was throwing that exception when you serialize your object, struct...here are the answer that I found here on the codeProject site by mister Mark Salsbery:
" Attached to the serialized data is the name/version of the assembly that defines the type of the serialized data. The system attempts to use that same assembly to get the type info required to deserialize the data into an object of the same type[So it throws a exception saying something like this ''Server, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null'']. You can make the same assembly available on both ends
or use a SerializationBinder on the deserializing end."

Take a Look here for further more information http://msdn.microsoft.com/en-us/library/system.runtime.serialization.serializationbinder.aspx[^]

您尝试执行的操作不适用于任何类型,但对于值类型也是可能的,这些成员也是静态类型的成员,以相同的方式进行限制.
我建议您通过使用序列化和System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ^ ];该格式化程序支持System.Runtime.Remoting.Messaging.IRemotingFormatter,因此非常适合联网,并且设计为支持RPC,同时结构紧凑,可以提供合理的性能.

有关序列化概述,请参见 http://msdn.microsoft.com/zh-我们/library/aa349369(v=vs.90).aspx [ http://msdn.microsoft.com/en-us/library/ms973893.aspx [ ^ ].

—SA
What you''re trying to do will not work with any type, but possible with value type which members are also static types limited in the same way.
I would advise you to simplify things down by using serialization and System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx[^]; this formatter supports System.Runtime.Remoting.Messaging.IRemotingFormatter, so it is good for networking and designed to support RPC and at the same time compact enough to provide reasonable performance.

For serialization overview, see http://msdn.microsoft.com/en-us/library/aa349369(v=vs.90).aspx[^], http://msdn.microsoft.com/en-us/library/ms973893.aspx[^].

—SA