Convertir un tableau de structures en IntPtr

Je suis en train de convertir un tableau de la structure RECT (ci-dessous) dans un IntPtr, pour que je puisse envoyer le pointeur à l'aide de PostMessage à une autre application.

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;

    //lots of functions snipped here
}

//so we have something to send, in reality I have real data here
//also, the length of the array is not constant
RECT[] foo = new RECT[4]; 
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(foo[0]) * 4);
Marshal.StructureToPtr(foo, ptr, true); //-- FAILS

Cela donne une ArgumentException sur la dernière ligne ("La structure spécifiée doit être blittable ou avoir des informations de mise en page."). J'ai besoin d'obtenir en quelque sorte ce tableau de Rectangles vers une autre application à l'aide de PostMessage, donc j'ai vraiment besoin d'un pointeur à ces données.

Quelles sont mes options?

Mise à JOUR: Cela semble fonctionner:

 IntPtr result = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.RECT)) * foo.Length);
 IntPtr c = new IntPtr(result.ToInt32());
 for (i = 0; i < foo.Length; i++)
 {
     Marshal.StructureToPtr(foo[i], c, true);
     c = new IntPtr(c.ToInt32() + Marshal.SizeOf(typeof(Win32.RECT)));
 }

De NOUVEAU mis à JOUR pour corriger ce qui arbitre commenté.

source d'informationauteur Vegard Larsen | 2009-07-06