serializing from unmanaged structure to Stream in C++/CLI
I am learning C++/CLI and stuck with a problem. I have a header file that looks like
typedef struct _DATA_INFO {
WORD ONE
WORD TWO
WORD THREE
} DATA_INFO
public ref class ManagedDataInfo
{
DATA_INFO* info;
public ManagedDataInfo()
{
info开发者_高级运维=new DATA_INFO();
}
public void Write(Stream^ stream)
{
// stream.Write(content of info)
// here i want to write content of info to stream
}
}
Here I want to copy the content of info to the stream in the Write method but stuck how to do that.
You need this method: Marshal.StructureToPtr. This method will convert structure to sequence of bytes. Then you can write then using one of Stream.Write method.
Stream provides a generic view of a sequence of bytes. So that means you need to serialize the info object to a sequence of bytes. To make byte array use Marshal.Copy method. Hope this helps.
精彩评论