memcpy functionality? please help!

G

Guest

In C++ I use the memcpy function to copy a struct to a buffer, I have not
been able to find a method to do this in C#.

I will need to be able to do this for a struct with mixed types.

Example as follows:

public struct MyStruct
{
public int iOne;
public int iTwo;
public int iThree;
public string sOne;
};

I would want to copy the above data in the struct to a buffer of bytes.

Example:

byte[] bytes = new byte[1024];

Any ideas?
Thank you
G
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

In C++ I use the memcpy function to copy a struct to a buffer, I have
not been able to find a method to do this in C#.

I will need to be able to do this for a struct with mixed types.

Example as follows:

public struct MyStruct
{
public int iOne;
public int iTwo;
public int iThree;
public string sOne;
};

I would want to copy the above data in the struct to a buffer of bytes.

Example:

byte[] bytes = new byte[1024];

C# prefer more safe programming practices.

I think the best choice would be to use a MemoryStream and
wrap that in a BinaryWriter.

A more dirty solution:

private static byte[] ConvertStructToByte( object anything, ref
byte rawsize)
{
rawsize = (byte)Marshal.SizeOf( anything );
IntPtr buffer = Marshal.AllocHGlobal( rawsize );
byte[] rawdatas = new byte[ rawsize ];
Marshal.StructureToPtr( anything, buffer, true );
Marshal.Copy( buffer, rawdatas, 0, rawsize );
Marshal.FreeHGlobal( buffer );
return rawdatas;
}

Arne
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

In C++ I use the memcpy function to copy a struct to a buffer, I have
not been able to find a method to do this in C#.

I will need to be able to do this for a struct with mixed types.

Example as follows:

public struct MyStruct
{
public int iOne;
public int iTwo;
public int iThree;
public string sOne;
};

I would want to copy the above data in the struct to a buffer of bytes.

Example:

byte[] bytes = new byte[1024];

Any ideas?
Thank you
G

If you would copy the data in the structure, it would not be usable. The
string is a reference type, so you would only copy the reference, not
the string data. Furthermore, the copy of the reference would not be a
reference, only a pointer, so it would point to the location of the
string at the moment when it was copied, but the string may have moved
to another location after that.

As Arne suggested, a BinaryWriter can be used for this.

There are other ways, like using the BitConverter class to get the int
values as bytes and the Encoding.GetBytes method to encode the string as
bytes.

It all depends on how you are planning to use the byte data.
 
G

Guest

Thank you for you responses.

My intent is to place the data in the structure into a buffer and then send
it on a TCP/IP stream.
I am proficient in C++, but I wanted to program the client software in C#,
due to the easy of programming the UI with .NET.

Maybe that will help to clarify my goal. My other question is, how efficient
is using BinaryWriter and Encoding.GetBytes. I do need low latency on the
client end for information transfer.

It seems unfortunate that C# does not make it easy to utilize
pointers/references and memory management. I understand why this is of
course in managed programming.

What about marshalling? Since my experience with C# is minimal, I would have
to ask. Would I still have the same problem you described of having only the
reference copied using marshalling?

What about a conversion of the structure to an array? Is this possible?
There seem to be good functions to manipulate and convert arrays to bytes
etc.

Thanks
G
 
T

Tiberiu Covaci

Hi G,

Using BinaryWriter and Encoding.GetBytes will do the trick, and based on the
encoding you have/need the string on the other side, you can choose the
right one in .NET.
To use pointers in C# you have to write unsafe code, but by doing so will
not be a nicer/easier/safer solution from a .NET point of view... Anyway
googling for unsafe will show you how.

Regards,
Tibi
MCT, MCPD

Thank you for you responses.

My intent is to place the data in the structure into a buffer and then
send it on a TCP/IP stream.
I am proficient in C++, but I wanted to program the client software in C#,
due to the easy of programming the UI with .NET.

Maybe that will help to clarify my goal. My other question is, how
efficient is using BinaryWriter and Encoding.GetBytes. I do need low
latency on the client end for information transfer.

It seems unfortunate that C# does not make it easy to utilize
pointers/references and memory management. I understand why this is of
course in managed programming.

What about marshalling? Since my experience with C# is minimal, I would
have to ask. Would I still have the same problem you described of having
only the reference copied using marshalling?

What about a conversion of the structure to an array? Is this possible?
There seem to be good functions to manipulate and convert arrays to bytes
etc.

Thanks
G


In C++ I use the memcpy function to copy a struct to a buffer, I have not
been able to find a method to do this in C#.

I will need to be able to do this for a struct with mixed types.

Example as follows:

public struct MyStruct
{
public int iOne;
public int iTwo;
public int iThree;
public string sOne;
};

I would want to copy the above data in the struct to a buffer of bytes.

Example:

byte[] bytes = new byte[1024];

Any ideas?
Thank you
G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top