C# and 'memory buffers'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Smith

    C# and 'memory buffers'

    I've been working with C++ for years and am strugling at C#. I am trying to
    reuse a COM object that I wrote that takes a chacter buffer and a size of
    the buffer. I am having a problem "filling" in the memory buffer to pass to
    my COM object. An example might be more explanatory.

    public struct Data
    {
    ushort data_id;
    ushort data_size;
    }

    Data my_data;
    sbyte buf = new sbyte[8];

    How do I copy the data structure into the buf array. Note that my data
    structure can be any structure and can be variable length, thats why I can't
    just to a simple ushort array. In C++ I would do something like this,
    memcpy( buf, my_data, my_data.data_si ze ). I really can't seem to get my
    mind around how to do this in C#.

    Any help would be greatly appreciated.

    Mike


  • Chris Dunaway

    #2
    Re: C# and 'memory buffers'

    Mike Smith wrote:
    I've been working with C++ for years and am strugling at C#. I am trying to
    reuse a COM object that I wrote that takes a chacter buffer and a size of
    the buffer. I am having a problem "filling" in the memory buffer to pass to
    my COM object. An example might be more explanatory.
    >
    public struct Data
    {
    ushort data_id;
    ushort data_size;
    }
    >
    Data my_data;
    sbyte buf = new sbyte[8];
    >
    How do I copy the data structure into the buf array. Note that my data
    structure can be any structure and can be variable length, thats why I can't
    just to a simple ushort array. In C++ I would do something like this,
    memcpy( buf, my_data, my_data.data_si ze ). I really can't seem to get my
    mind around how to do this in C#.
    Have a look at Marshal.Copy in the System.Runtime. InteropServices
    namespace.

    Comment

    • Chris Dunaway

      #3
      Re: C# and 'memory buffers'

      Mike Smith wrote:
      How do I copy the data structure into the buf array. Note that my data
      structure can be any structure and can be variable length, thats why I can't
      just to a simple ushort array. In C++ I would do something like this,
      memcpy( buf, my_data, my_data.data_si ze ). I really can't seem to get my
      mind around how to do this in C#.
      Actually Marshal.Structu reToPtr and Marshal.PtrToSt ructure would
      probably be more helpful.

      Comment

      • Mike Smith

        #4
        Re: C# and 'memory buffers'

        Since there were no replies I can only assume that C++ is a more "superior"
        language than C#? Just poking fun. Does any have an idea where I can
        start?

        "Mike Smith" <MikeSmith@some where.comwrote in message
        news:OrS9Nu2AHH A.4256@TK2MSFTN GP04.phx.gbl...
        I've been working with C++ for years and am strugling at C#. I am trying
        to reuse a COM object that I wrote that takes a chacter buffer and a size
        of the buffer. I am having a problem "filling" in the memory buffer to
        pass to my COM object. An example might be more explanatory.
        >
        public struct Data
        {
        ushort data_id;
        ushort data_size;
        }
        >
        Data my_data;
        sbyte buf = new sbyte[8];
        >
        How do I copy the data structure into the buf array. Note that my data
        structure can be any structure and can be variable length, thats why I
        can't just to a simple ushort array. In C++ I would do something like
        this, memcpy( buf, my_data, my_data.data_si ze ). I really can't seem to
        get my mind around how to do this in C#.
        >
        Any help would be greatly appreciated.
        >
        Mike
        >

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: C# and 'memory buffers'

          Can you show us your COM interface definition, IDL or tlb is what we are
          looking for. The reason is that you probably don't need to copy anything,
          this is something that is taken care of by the COM interop layer in the CLR,
          unless you pass arguments that cannot be handled by the COM layer, in which
          case you'll need to implement custom marshalin.

          Willy.


          "Mike Smith" <MikeSmith@some where.comwrote in message
          news:%23HzpOPMC HHA.4348@TK2MSF TNGP04.phx.gbl. ..
          | Since there were no replies I can only assume that C++ is a more
          "superior"
          | language than C#? Just poking fun. Does any have an idea where I can
          | start?
          |
          | "Mike Smith" <MikeSmith@some where.comwrote in message
          | news:OrS9Nu2AHH A.4256@TK2MSFTN GP04.phx.gbl...
          | I've been working with C++ for years and am strugling at C#. I am
          trying
          | to reuse a COM object that I wrote that takes a chacter buffer and a
          size
          | of the buffer. I am having a problem "filling" in the memory buffer to
          | pass to my COM object. An example might be more explanatory.
          | >
          | public struct Data
          | {
          | ushort data_id;
          | ushort data_size;
          | }
          | >
          | Data my_data;
          | sbyte buf = new sbyte[8];
          | >
          | How do I copy the data structure into the buf array. Note that my data
          | structure can be any structure and can be variable length, thats why I
          | can't just to a simple ushort array. In C++ I would do something like
          | this, memcpy( buf, my_data, my_data.data_si ze ). I really can't seem to
          | get my mind around how to do this in C#.
          | >
          | Any help would be greatly appreciated.
          | >
          | Mike
          | >
          |
          |


          Comment

          • Chris Dunaway

            #6
            Re: C# and 'memory buffers'

            Mike Smith wrote:
            Since there were no replies I can only assume that C++ is a more "superior"
            No replies? I pointed out this:

            "Actually Marshal.Structu reToPtr and Marshal.PtrToSt ructure would
            probably be more helpful. "

            Comment

            Working...