Joe,
The Buffer class is in the System namespace, and is in the mscorlib.dll
assembly, so it is available to you by default.
The only issue is knowing how large the structure is going to be. You
can determine this programatically by using the sizeof operator in unsafe
code. You might be tempted to use the static SizeOf method on the Marshal
class. I would recommend against that, because the size of the structure in
bytes on the unmanaged end is not always the same as the structure on the
managed end.
You can refer to the sizeof operator documentation to determine the size
of the structure:
http://msdn.microsoft.com/library/de...spec_A_5_8.asp
Your code should look like this:
// The structure we want to transform into bytes.
MyStruct pobjStruct = new MyStruct();
// Do some work to set the values. Now initialize the bytes.
// pintNumberOfBytesNeededForStructure is the number of bytes that the
structure takes up in memory.
byte[] pbytBytes = new byte[pintNumberOfBytesNeededForStructure];
// Create an array of the structures, make it one element long, and set to
your struct.
MyStruct[] pobjStructArray = new MyStruct[1];
// Set the value in the structure.
pobjStructAway[0] = pobjStruct;
// Copy the structure into the byte array.
Buffer.BlockCopy(pobjStructArray, 0, pbytBytes, 0,
pintNumberOfBytesNeededForStructure);
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Joe Thompson" <an*******@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
Hi Nicholas,
Thank you for the reply. My structure is self contained. I don't
understand what you mean by: "call the static BlockCopy method on the Buffer class"
What is the Buffer class? I don't see a method called BlockCopy in C#
anywhere...
Thank you,
Joe