Can someone explain the marshal. StructureToPtr

I am having problems with this code:

//Creating a new ImageElement Struct
ImageElement oElement = new UM0516.ImageElement();
//Create a pointer and allocate enough room for the struct type
IntPtr pElement = Marshal.AllocHGlobal(Marshal.SizeOf(new UM0516.ImageElement()));
//Copy the contents of the struct into the allocated memory space
Marshal.StructureToPtr(oElement, pElement, true);
//Function that takes a file pointed to by handle, and does some sweet sweet things
//And returns a loaded struct pointed to by pElement
FILES_GetImageElement(handle, el, out pElement);

This is where I got confused: I will go through the code, and after I call this last function (which should change some bits in memory pointed to by pElement), I see a change in oElement !? I thought that Marshal.StructureToPtr "copies" the data from the managed structure into memory. Similarly, are both places actually the same? The oElement managed structure and allocated memory pointed to by pElement?

+3
source share
2 answers

This article explains this in detail :

blittable () . , . , .

+3

, , , . , .

pElement oElement ref ( , ), out.

0

Source: https://habr.com/ru/post/1706204/


All Articles