在C#问题中未管理到托管结构 [英] Unmanaged to managed structs in C# problem
本文介绍了在C#问题中未管理到托管结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'm receiving in c# a structure pointer from a c++ library callback
c++ structure
the packing is 1
typedef
{
int data1[8];
int data2[8];
int data3;
int data3;
} SomeStruct;
in c# i have this equivalent
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1), Serializable]
public struct SomeStruct
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public int[] data1
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public int [] data2;
public int data3;
public int data4;
};
我尝试过:
What I have tried:
var data contains the right IntPtr
SomeStruct ss = (SomeStruct )Marshal.PtrToStructure(pointer, typeof(SomeStruct));
But im getting garbage, what im doing wrong?, thanks
推荐答案
您是否尝试过UnmanagedType。 LPArray属性?
Have you tried the UnmanagedType.LPArray attribute?
[MarshalAs(UnmanagedType.LPArray, SizeConst = 8)]
public int[] data1
这里的例子使用它:数组的默认封送 [ ^ ] - 搜索何时导入C风格的数组并查看Managed Signature下的示例
The example here uses it: Default Marshaling for Arrays[^] - search for "When a C-style array is imported" and look at the example under "Managed Signature"
这篇关于在C#问题中未管理到托管结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文