Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

C# and 'memory buffers'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Smith - 08 Nov 2006 19:24 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
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. "

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.