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_size ). 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 - 08 Nov 2006 22:20 GMT
> 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
[quoted text clipped - 15 lines]
> memcpy( buf, my_data, my_data.data_size ). 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.
Chris Dunaway - 08 Nov 2006 22:21 GMT
> 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_size ). I really can't seem to get my
> mind around how to do this in C#.
Actually Marshal.StructureToPtr and Marshal.PtrToStructure would
probably be more helpful.
Mike Smith - 15 Nov 2006 14:39 GMT
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?
> 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
[quoted text clipped - 19 lines]
>
> Mike
Willy Denoyette [MVP] - 15 Nov 2006 15:17 GMT
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.
| 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
[quoted text clipped - 23 lines]
| >
| > Mike
Chris Dunaway - 15 Nov 2006 22:30 GMT
> Since there were no replies I can only assume that C++ is a more "superior"
No replies? I pointed out this:
"Actually Marshal.StructureToPtr and Marshal.PtrToStructure would
probably be more helpful. "