从 IntPtr 获取结构体数组
我有一些像这样的结构
struct MyStruct
{
public int field1;
public int field2;
public int field3;
}
,并且有指向该结构数组的指针。 所以,我需要从这个指针获取数组。 我尝试使用 Marshal.PtrToStructure,但出现内存读取错误。 这是我的方法:
public MyStruct[] GetArrayOfStruct(IntPtr pointerToStruct, int length)
{
var sizeInBytes = Marshal.SizeOf(typeof(TCnt));
MyStruct[] output = new MyStruct[length];
for (int i = 0; i < length; i++)
{
IntPtr p = new IntPtr((pointerToStruct.ToInt32() + i * sizeInBytes));
output[i] = (MyStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(MyStruct));
}
return output;
}
那么,我做错了什么?
I have some struct like this
struct MyStruct
{
public int field1;
public int field2;
public int field3;
}
and I have pointer to array of this struct.
So, I need to get array from this pointer.
I'm tried to using Marshal.PtrToStructure, but i had memory reading error.
This is my methode:
public MyStruct[] GetArrayOfStruct(IntPtr pointerToStruct, int length)
{
var sizeInBytes = Marshal.SizeOf(typeof(TCnt));
MyStruct[] output = new MyStruct[length];
for (int i = 0; i < length; i++)
{
IntPtr p = new IntPtr((pointerToStruct.ToInt32() + i * sizeInBytes));
output[i] = (MyStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(p, typeof(MyStruct));
}
return output;
}
So, what am i doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设结构的大小是固定的,这个函数对我有用:
This function worked for me, assuming that the size of the struct is fixed:
两个问题。您在 Marshal.SizeOf() 调用中使用 TCnt 而不是 MyStruct。您的 IntPtr 算术无法在 64 位计算机上运行,您必须使用 IntPtr.ToInt64() 或强制转换为 (long)。
当然,得到错误的 IntPtr 或长度也是有可能的。使用 Debug + Windows + Memory + Memory 1 并将“pointerToStruct”放入地址框中进行基本验证。
Two problems. You use TCnt instead of MyStruct in the Marshal.SizeOf() call. Your IntPtr arithmetic cannot work on a 64-bit machine, you must use IntPtr.ToInt64() or cast to (long).
Just getting the wrong IntPtr or length is certainly a possibility too of course. Use Debug + Windows + Memory + Memory 1 and put "pointerToStruct" in the Address box for basic verification.
C 和 C# 中的结构不是一回事。区别之一是,在 C# 中,您必须明确要求您的结构应按顺序布局。如果你没写
[StructLayout(LayoutKind.Sequential)]
或[StructLayout(LayoutKind.Explicit)]
属性到您的结构我不相信您可以通过这种方式管理它。 Microsoft 声明PtrToStructure
用于将结构从非托管内存转换为托管内存。您应该测试将此属性添加到结构中是否有帮助,如果还没有帮助,请尝试使用
Marshal 分配内存.AllocHGlobal(IntPtr)
并使用Marshal.Copy
初始化您的结构,然后尝试使用PtrToStructure
。如果这有效,那么您不能将PtrToStructure
与托管内存一起使用Structs in C and C# are not the same thing. One of the differences is that in C# you have to explicitly demand that your struct should be sequentially laid out. If you didn't write
[StructLayout(LayoutKind.Sequential)]
or[StructLayout(LayoutKind.Explicit)]
attribute to your structure I don't believe that you can manage it in this way. Microsoft states thatPtrToStructure
is to be used to convert structures from unmanaged to managed memoryYou should test if adding this attributes to your struct helps, If it doesn't yet help try allocating memory with
Marshal.AllocHGlobal(IntPtr)
and useMarshal.Copy
to init your structure and then try usingPtrToStructure
. If this works then you can't usePtrToStructure
with managed memory