【发布时间】:2009-04-09 21:15:35
【问题描述】:
我在处理这段代码时遇到了一些问题:
//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 将数据从托管结构“复制”到内存。那么这两个位置实际上是一样的吗? pElement 指向的托管结构 oElement 和分配的内存?
【问题讨论】:
-
-1 用于在同一篇文章中写“Helps YO”和“Coo Dawg”。
标签: memory struct pointers marshalling