Last active
April 5, 2021 12:00
-
-
Save GTANAdam/77604a4924d48a9acd3eaa0b8c9e0819 to your computer and use it in GitHub Desktop.
Marshalling a pointer to an array of structures (C/C++/C# Interop)
This file contains hidden or 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
| public unsafe static void MarshalPtrToStructureArray<T>(IntPtr sPtr, ulong arrSize, ref T[] output) where T : unmanaged | |
| { | |
| fixed (T* dPtr = output) | |
| { | |
| ulong dataSize = arrSize * (uint)Marshal.SizeOf(typeof(T)); | |
| Unsafe.CopyBlock(dPtr, (void*)sPtr, (uint)dataSize); | |
| // Alternatively, .NET Framework >= 4.6 | |
| // ulong dataSize = arrSize * (uint)Marshal.SizeOf<T>(); | |
| // Buffer.MemoryCopy((void*)sPtr, dPtr, dataSize, dataSize); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment