Last active
May 12, 2021 07:23
-
-
Save gregseth/4977109 to your computer and use it in GitHub Desktop.
Conversion of an unmanaged to a managed array of struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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