When I use IntPtr to reserve memory and pass a dynamic array to native code, after I initialize this memory on the C#/managed side and pass it to my native DLL, is this chunk of memory pinned or copied? I.e. if I modify the array within my native code, will I see the modifications back in my managed code?
I know it is not necessary to use IntPtr, but since the array is embedded into a complex struct, it seems more convenient.
Marshal.AllocHGlobal
? It allocates native non-movable memory block and returnsIntPtr
pointing to it. If you pass thisIntPtr
to native function, and it changes the memory, C# can see these changes. The memory is not released automatically, useMarshal.FreeHGlobal
for this. – Alex FMarshal.AllocHGlobal
is ALWAYS pinned. – sapito