On Jun 24, 10:52 am, Sharon <Shar...@newsgroups.nospamwrote:
I have the following managed C++ function (VC++ 2005 (C++/CLI)
System::Array^ ManagedCppClass::GetData()
{
BYTE* pData;
int len;
m_pureNativeCPPObj->GetData(pData, len); // Get the data buffer from
unmanaged class.
array<byte>^ Arr = gcnew array<byte>(len);
System::Runtime::InteropServices::Marshal::Copy((I ntPtr)pAScan, Arr, 0,
len);
return Arr;
}
I think this function works.
But is there a better way to that (maybe without doing the copy) ?
If your native API allows you to get the data size without retrieveing
the actual data, AND to use a user-supplied buffer, you could :
- get the data size from native API.
- Allocate a managed array of the correct size.
- Pin that array (using pin_ptr).
- Pass the pinned pointer to the native API, so that it fills directly
the managed array.
- Return the managed array.
This may be more efficient if the data is big (if the data is small,
it won't make any difference - it may even make performance worst
because it means having 2 managed/native transitions instead of one).
Also, it requires that you change the unmanaged API, but this may be a
good idea anyway, since it will make your API more similar to Win32
API.
Arnaud
MVP - VC