memcpy functionality? please help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    memcpy functionality? please help!

    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?=

    #2
    Re: memcpy functionality? please help!

    gsx@gsxnet.com wrote:
    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[] ConvertStructTo Byte( object anything, ref
    byte rawsize)
    {
    rawsize = (byte)Marshal.S izeOf( anything );
    IntPtr buffer = Marshal.AllocHG lobal( rawsize );
    byte[] rawdatas = new byte[ rawsize ];
    Marshal.Structu reToPtr( anything, buffer, true );
    Marshal.Copy( buffer, rawdatas, 0, rawsize );
    Marshal.FreeHGl obal( buffer );
    return rawdatas;
    }

    Arne

    Comment

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

      #3
      Re: memcpy functionality? please help!

      gsx@gsxnet.com wrote:
      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.GetByt es method to encode the string as
      bytes.

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

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Guest's Avatar

        #4
        Re: memcpy functionality? please help!

        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.GetByt es. 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


        <gsx@gsxnet.com wrote in message
        news:eYK4hX7jHH A.3264@TK2MSFTN GP04.phx.gbl...
        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

        Comment

        • Tiberiu Covaci

          #5
          Re: memcpy functionality? please help!

          Hi G,

          Using BinaryWriter and Encoding.GetByt es 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

          <gsx@gsxnet.com wrote in message
          news:ueAPdZAkHH A.2552@TK2MSFTN GP06.phx.gbl...
          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.GetByt es. 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
          >
          >
          <gsx@gsxnet.com wrote in message
          news:eYK4hX7jHH A.3264@TK2MSFTN GP04.phx.gbl...
          >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
          >

          Comment

          Working...