积极答复者
API函数中结构指针参数实际指向结构数组时,在C#中如何使用

问题
答案
-
[StructLayout(LayoutKind.Sequential)] public struct SA {public int x; public float y;} public partial class NativeMethods { [DllImport("<Unknown>", EntryPoint="GetSA", CallingConvention=CallingConvention.StdCall)] public static extern int GetSA(IntPtr psa, int nCount) ; } IntPtr psa= Marshal.AllocCoTaskMem (Marshal.SizeOf(SA)*count); try { GetSA(psa,count); for(int i=0;i<count;i++) { SA item= (SA)Marshal.PtrToStructure(psa.Add(Marshal.SizeOf(SA)*i) , typeof(SA)); ...... } } finally { Marshal.FreeCoTaskMem(psa); }
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Paul Zhou 2011年7月27日 6:35
全部回复
-
例如 有结构体
struct SA {int x, float y};
有第三方API函数
psa是一个指向SA结构的一个数组
nCount是输入参数psa的最大个数
返回值是取得的SA结构实际数目
int __stdcall GetSA(SA *psa, int nCount);
C++中用法:
SA sa[10];
int nCount = GetSA(sa, 10);
// sa处理
C#中如何使用
int __stdcall GetSA(SA *psa, int nCount);函数
- 已合并 Sheng Jiang 蒋晟Moderator 2011年7月19日 20:35
-
[StructLayout(LayoutKind.Sequential)] public struct SA {public int x; public float y;} public partial class NativeMethods { [DllImport("<Unknown>", EntryPoint="GetSA", CallingConvention=CallingConvention.StdCall)] public static extern int GetSA(IntPtr psa, int nCount) ; } IntPtr psa= Marshal.AllocCoTaskMem (Marshal.SizeOf(SA)*count); try { GetSA(psa,count); for(int i=0;i<count;i++) { SA item= (SA)Marshal.PtrToStructure(psa.Add(Marshal.SizeOf(SA)*i) , typeof(SA)); ...... } } finally { Marshal.FreeCoTaskMem(psa); }
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP- 已标记为答案 Paul Zhou 2011年7月27日 6:35