C# and 'memory buffers'

M

Mike Smith

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
 
C

Chris Dunaway

Mike said:
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#.

Have a look at Marshal.Copy in the System.Runtime.InteropServices
namespace.
 
C

Chris Dunaway

Mike said:
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.
 
M

Mike Smith

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

Willy Denoyette [MVP]

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
| 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
| > 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
| >
|
|
 
C

Chris Dunaway

Mike said:
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. "
 

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