我在使用这段代码时遇到了一些问题:
//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);
这是我感到困惑的地方:我将逐步执行代码,在我调用最后一个函数(它应该更改 pElement 指向的内存中的一些位)之后,我看到 oElement 发生了变化!?我认为 Marshal.StructureToPtr 将数据从托管结构“复制”到内存。那么这两个位置实际上是一样的吗?托管结构oElement和pElement指向的分配内存?