best way to pass block of data from C# to C++?

  • Thread starter Thread starter Doug Taylor
  • Start date Start date
D

Doug Taylor

Hi. I'm new here and I don't think I'm in Kansas anymore...

What's the best / most efficient way to marshal a buffer of pixel
data from a C# COM object up to a C++ application? Anybody got a
"short but complete program" to illustrate it?
 
Hey Doug,

1/ You can try to pin the memory. For instance if you have a byte[], you can
pin it so it doesn't get moved around in memory.

2/ Alternatively you can use the GCHandle class to pin the memory.

3/ You could use AllocCoTaskMem & FreeCoTaskMem methods in the Marshal class
(but I've never tried).

4/ Use another way to allocate memory than .NET memory manager. For
instance, link to VirtualAlloc and VirtualFree in kernel32.dll

5/ Have your C++ component give a pointer to a memory block which can be
filled by the C# component.

HTH,
Tom Tempelaere.
 
I forgot to mention that the app is VC6, and not VC++.NET.
Thus, is your suggestion #5 really possible?

The VC6 app will be allocating the buffer, then the C# object
will fill the buffer (or a copy of the buffer if that's the only
way),
and then return it back to the VC6 app.

Doug

TT (Tom Tempelaere) said:
1/ You can try to pin the memory. For instance if you
have a byte[], you can pin it so it doesn't get moved
around in memory.

2/ Alternatively you can use the GCHandle class to pin
the memory.

3/ You could use AllocCoTaskMem & FreeCoTaskMem methods
in the Marshal class (but I've never tried).

4/ Use another way to allocate memory than .NET memory
manager. For instance, link to VirtualAlloc and
VirtualFree in kernel32.dll

5/ Have your C++ component give a pointer to a memory
block which can be filled by the C# component.

HTH, Tom Tempelaere.

Doug Taylor said:
What's the best / most efficient way to marshal a buffer
of pixel data from a C# COM object up to a C++
application? Anybody got a "short but complete
program" to illustrate it?
 
Back
Top