I'm still not sure this is the right way to do it. Here's some more
information on the structs I am trying to build from the byte[].
The structure contains nested structures. The nested structures are
interesting because they are defined in a COM type library. I am using them
in my C# project by means of the tlbimp.exe that converts a COM type library
into a common language runtime assembly. I am referencing this assembly in
my C# project. The byte arrays in the nested structures are defined, for
example, as:
BYTE strBarcode[32]; // This is in the idl file that is compiled to COM
type library.
When I inspect these members in the object viewer after importing the
library into a runtime assembly they show up as, for example:
public byte[] strBarcode
I convert the structures to byte arrays in a C++ server program running on a
remote machine, and my C# client app reads them in and HOPEFULLY someday
will be able to fill up a struct identical to the one it originated from.
The CLR should know how big strBarcode is, right? If it does, I can't
understand why I can't simply do something like:
unsafe
{
MyStruct* p = &MyStruct;
Marshal.Copy(byteArray, (IntPtr)p, ....)
}
When I try code like this the compiler complains that I am trying to use the
address of a managed struct. If I remove those byte[] members from the
struct, the code compiles fine.
So my question remains, how can I simply load up my structure from a byte
stream? This seems like it should be so easy!
Ben
Justin Rogers said:
What John suggested only works if the byte[] contains serialized
data. What you actually want to look at is unsafe code. You can
use MarshalPtrToStructure and MarshalStructureToPtr as long as
your struct has layout information.
Note the above two methods aren't necessarily unsafe because the
CLR implements them for you, but they do require high privs, and
if I'm not mistaken they do require unmanaged code permissions.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog:
http://weblogs.asp.net/justin_rogers
Ben Terry said:
Thanks for the help, John. I've tried what you suggested:
MemoryStream ms = new MemoryStream(m_ReceivedBytes);
BinaryFormatter bf = new BinaryFormatter();
m_CompleteIllumStateMsg = (MIX_CompleteIllumStateMsg)bf.Deserialize(ms);
Where m_ReceivedBytes is a byte[] and m_CompleteIllumStateMsg is my
structure. I get the following exception when I try to Deserialize:
"BinaryFormatter Version incompatibility. Expected Version 1.0. Received
Version 43.0." } System.Exception
Any thoughts on how to resolve this?
Ben
Sorry you wanted the reverse... in which case you can create a
MemoryStream
on the byte[] and use the Deserialize method of the BinaryFormatter to
create the struct from the buffer.
--
John Wood
EMail: first name, dot, second name at priorganize.com
What's the most efficient way to transfer data from a byte[] to a
struct?
The struct is rather complex--contains other structs as well as byte[]
members. I've tried to use Marshal.Copy and an IntPtr to my struct
address
but I get the following error, perhaps due to the byte[] members
of