C接口struct PointVec3f{//储存XYZ的坐标值.
float xValue;
float yValue;
float zValue;
};
extern "C" __declspec(dllimport) void getPointCloud(PointVec3f *points, int pointNum);C#定义
public struct PointVec3f  
        {
            [MarshalAs(UnmanagedType.R4)]
            public float xValue;
            [MarshalAs(UnmanagedType.R4)]
            public float yValue;
            [MarshalAs(UnmanagedType.R4)]
            public float zValue;
        };
PointVec3f[] points =new PointVec3f[NUM];  //points结构体数组的值我已经获取到了,想请问怎么传指针到PointVec3f *points中!Ref的方法试过了,不行!有没有其他方法,急!!         private void button1_Click(object sender, EventArgs e)
        {
          StreamReader strs = new StreamReader("扫描结果.txt",Encoding.Default);
          String [] s;
          int i;
          int j;
          j = 0;
          DisPlay.PointVec3f[] points = new DisPlay.PointVec3f[256000];
          // 结构体类型和IntPtr类型的大小
          int nSizeofRD = Marshal.SizeOf(typeof(DisPlay.PointVec3f));
          int nSizeofIntPtr = Marshal.SizeOf(typeof(IntPtr));          // 转换为指针,首先建立一个与之同样大小的数组
          IntPtr[] pArray = new IntPtr[points.Length];
          //for (int i = 0; i < rData.Length; i++)
          pArray[0] = Marshal.AllocHGlobal(nSizeofRD * points.Length);          // 将数组转换为一个指针
          IntPtr pRD = Marshal.AllocHGlobal(nSizeofIntPtr * 1);          // 把C#结构体数组拷贝至这个指针
          Marshal.Copy(pArray, 0, pRD, 1);
          for (int k = 0; k < points.Length; k++)
              Marshal.StructureToPtr(points[k], (IntPtr)((UInt32)pRD + k * nSizeofRD), true);          dissplay = new DisPlay();
          dissplay.init();
          string str = "";
          for (i = 0; i < NUM; i++)
          {
              str = strs.ReadLine();
              if ((s = str.Split(' ')) == null)
              break;
              if ((s = str.Split(' ')) != null && "Profile" == s[0])
              {
                  strs.ReadLine();
              }
              else
              {
                  points[j].xValue = float.Parse(s[1]);
                  points[j].yValue = float.Parse(s[3]);
                  points[j].zValue = float.Parse(s[2]);
                  j++;
                  Num++;
              }
          }
         DisPlay.getPointCloud( pRD , Num );
         DisPlay.show(this.Handle);

解决方案 »

  1.   

    有一个帖子跟我的情况相似(http://bbs.csdn.net/topics/330074372),但是我是新手看不懂,不知道如何改。根据他的改了报错了
      

  2.   

    你可以直接传入数组,数组是引用类型,PInvoke时相当于传地址。
    void getPointCloud(DisPlay.PointVec3f[] points, int pointNum);
      

  3.   

    为什么我用Ref引用类型传值的时候只传了结构体数组的第一个数据呢?