How to convert System.Array to IntPtr

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have a question confused me a long time.
I am working on DirectDraw .NET a while.
And I want to copy data to surface, but I found Surface.Lock return
System.Array.
I tried to cast it to byte[],byte[,], but in vain( It will crash without any
exception).

Before I saw somebody said he used unsafe code to slove the problem on
newsgroup, but without any sample codes.

Can anyone give me some suggestion or ways to slove it? thanks a lot.
 
zishen,

If you want to get the pointer to an array, you can do this:

// Unsafe code.
unsafe
{
// Fix the pointer to the array. Assume array is the variable and T is
the type of the array.
fixed (T* pArray = array)
{
// pArray now has the pointer to the array. You can get an IntPtr
by casting to void, and passing that in.
IntPtr intPtr = new IntPtr((void *) pArray);
}
}

That should work.

Hope this helps.
 
Back
Top