unmanaged pointer to managed array

M

MR

I am new to VS.C++ and am having a hard time when i have to share data
between managed and unmanaged code.
For example, I need to read binary data from a file and then call an non-CLR
dll function passing the data that was read as a void*
below is a snippet of the code that i have written in an attempt to do this.
I tried __pin but that is no longer supported in VS.2005.

is there a better way than reading into the array as I did?
How do you do this?
thanks for your help
m


struct UnmanagedValues // create a struct to hold all nonManaged values.
{
void * buffer;
} unManagedValues;
array<wchar_t>^ charBuffer = r->ReadChars(bufferSize); // read the data
into a managed array.
unManagedValues.buffer = &charBuffer; //but now I need to set the address of
the array as a void*. of course this line generates an error
 
W

William DePalo [MVP VC++]

MR said:
I am new to VS.C++ and am having a hard time when i have to share data
between managed and unmanaged code.

You can use the wrapper template gcroot that appears in <vcclr.h>.
Essentially it gets you a handle to an allocation on the garbage collected
heap, and through some template magic allows you to "use" the handle as a
pointer in native code.

Details here:

http://msdn.microsoft.com/library/d...cmxspec/html/vcManagedExtensionsSpec_16_3.asp

http://msdn.microsoft.com/msdnmag/issues/05/04/C/

Regards,
Will
 
A

adebaene

MR a écrit :
I am new to VS.C++ and am having a hard time when i have to share data
between managed and unmanaged code.
For example, I need to read binary data from a file and then call an non-CLR
dll function passing the data that was read as a void*
below is a snippet of the code that i have written in an attempt to do this.
I tried __pin but that is no longer supported in VS.2005.

You should use the new equivalent : cli::pin_ptr

is there a better way than reading into the array as I did?
How do you do this?
thanks for your help
m


struct UnmanagedValues // create a struct to hold all nonManaged values.
{
void * buffer;
} unManagedValues;
array<wchar_t>^ charBuffer = r->ReadChars(bufferSize); // read the data
into a managed array.
unManagedValues.buffer = &charBuffer; //but now I need to set the address of
the array as a void*. of course this line generates an error

use :
cli::pin_ptr<wchar_t> ptr= &charBuffer[0];
void* ptr2=ptr;
unManagedValues.buffer=ptr2;

Arnaud
MVP - VC
 

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