Intptr是Struct数组的起始地址,如何获取数组中其他Struct值
调用C++方法返回数组首地址
IntPtr returnIntPtr = PictureMatching.GetMatchingResult(ref result_size,sbSimpleFilePath.ToString(),sbSelectedPictruePath.ToString(), intMatchingType, rect);
PicMatchingResult testPicMatchingResult = (PicMatchingResult)Marshal.PtrToStructure(returnIntPtr, typeof(PicMatchingResult));
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsFormsApplication14{ struct S { public int a; public int b; } public partial class Form1 : Form { public Form1() { InitializeComponent(); S[] s = new S[2] { new S() { a=1, b=11 }, new S() { a=2, b=22 } }; //for (int i = 0; i < 2; i++) //{ // IntPtr P = Marshal.UnsafeAddrOfPinnedArrayElement(s, i); // S A = (S)Marshal.PtrToStructure(P, typeof(S)); // MessageBox.Show("a=" + A.a + " b=" + A.b); //} IntPtr P = Marshal.UnsafeAddrOfPinnedArrayElement(s, 0); S A = (S)Marshal.PtrToStructure(P, typeof(S)); MessageBox.Show("a=" + A.a + " b=" + A.b); P = new IntPtr(P.ToInt32() + Marshal.SizeOf(A)); A = (S)Marshal.PtrToStructure(P, typeof(S)); MessageBox.Show("a=" + A.a + " b=" + A.b); } }}