dll说明如下:C formatint32 pisn_getsnapshots( int32 PIPTR * ptfloat PIPTR * rval, int32 PIPTR * istat,int32 PIPTR * timedate, int32 PIPTR * error,int32 count ); Argumentspt (passed)Array of point numbersrval (returned)Array of values in engineering units, undefined for integer and digital pointsistat (returned)Array of statuses for type real points and values for integer and digital pointstimedate (returned)Array of time stampserror (returned)Array of error codescount(passed)Size of the arrays
我定义成下面的,但是调用的时候会出现各种异常,其中包括:
1.尝试读取或写入受保护的内存。
2.“System.ExecutionEngineException”的异常。
3.签名类型不符。
4.堆栈溢出。
[DllImport("piapi32.dll")]
public extern static int pisn_getsnapshots(int[] pt, ref float[] rval, ref int[] istat, ref int[] timedate, ref int[] error, int count);

解决方案 »

  1.   

    第一个和最后一个参数都是传入,其它参数是传出的,也就是函数要在中间几个指针指示的位置写内容,根据错误提示,很明示是这几个参数出了问题,不能正确写内存从上面的参数说明不能知道数组大小,否则可能产生越界或溢出要传递一个数组让函数写上内容,要申请非托管的内存,假如第二个参数数组大小是5:IntPtr rval = AllocHGlobal(sizeof(float)*5);
    将rval直接传给第二个参数,就能让函数数据了你用的ref float[] rval,请问这个float数组长度是多少? 定义这个长度的依据是什么?
      

  2.   


    ref float[] rval  的数组长度是根据 int[] pt 的长度决定的,因为着方法是一个pt对应一个rval。可是我把传入的Count参数设置为比 int[] pt 的长度大的时候,异常1就不会出现了,就会异常2。
      

  3.   

    count 应该是数组长度罗?
    int count = 5;
    int[] pt = new int[count];
    IntPtr rval = AllocHGlobal(sizeof(float)*count);
    IntPtr istat = AllocHGlobal(sizeof(int)*count);
    IntPtr timedate = AllocHGlobal(sizeof(int)*count);
    IntPtr error = AllocHGlobal(sizeof(int)*count);pisn_getsnapshots(pt[],rval,istat,timedate,error,count);
    试试