المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Managed/Unmanaged code intraoperability: Casting between pointers



C# Programming
07-17-2010, 04:30 AM
Hello everyone,

I have been trying to cast some pointers in 'unsafe' code block but running into the following error:
error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('DataType')

My code is as follows:

protected unsafe int MyFunc(ref int nValues, ref DataType* values)
{
....
DataType[] valuesArr = new DataType[nValues];
GCHandle valuePtr = GCHandle.Alloc(valuesArr, GCHandleType.Pinned);
values = (DataType *)valuePtr.AddrOfPinnedObject().ToPointer(); // CS0208
}


This code is called from unmanaged side and basically I would like to cast the pointer from GCAlloc to the correct type... I am guessing it is not allowed even within an unsafe code block.

Just wondering if there is any way around this?

Many thanks,

Keith