Skip to content

Instantly share code, notes, and snippets.

@gregseth
Last active May 12, 2021 07:23
Show Gist options
  • Save gregseth/4977109 to your computer and use it in GitHub Desktop.
Save gregseth/4977109 to your computer and use it in GitHub Desktop.
Conversion of an unmanaged to a managed array of struct
/// <summary>
/// Méthode générique de conversion d'un pointeur sur une liste de
/// structures non managées, en un tableau managé de structures.
/// </summary>
/// <typeparam name="Struct">Le type de la structure</typeparam>
/// <param name="_P">Le pointeur vers le premier élément du tableau
/// non managé.</param>
/// <param name="_Size">La taille du tableau non managé.</param>
/// <returns>Le tableau managé contenant les structures.</returns>
Struct[] PtrToStructArray<Struct>(IntPtr _P, int _Size)
{
int nStructSize = Marshal.SizeOf(typeof(Struct));Struct[] array = new Struct[_Size];
IntPtr pWalker = _P;
for (int i=0; i<_Size; i++)
{
array[i] = (Struct)Marshal.PtrToStructure(pWalker, typeof(Struct));
pWalker += nStructSize;
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment