函数原型:
int func(int *p1, int p2) ;
p1返回一数组.在c#中声明 
    private static extern int func( ref int[] p1, int p2) ;
但调用后不能返回数组,使用p1[0]时出错。不知该如何处理?请指教

解决方案 »

  1.   

    you can use point in C# too.
    unsafe, this key word
      

  2.   

    直接用 ref p1的话只能得到一个整数,但函数原型返回是个数组。
      

  3.   

    用过IntPtr,但不知如何将其转换为int[]。
      

  4.   

    在c#中声明:
        private static extern int func( int[] p1, int p2) ;
    不用加关键字:ref调用函数时:
     int p2 = 10;
     int[] p1 = new int [20];
     func(p1, p2);
     func中对p1的修改有效。
    注意:一定得先int[] p1 = new int [20];,给p1引用初始化,否则p1=null,错误。
      

  5.   

    你的原型应该是
    private static extern int func( [In,Out] int[] p1, int p2) ;这种情况下数组长度不能变化.
      

  6.   

    测试了一下:C++ 函数:int func(int *p1, int p2)
    {
    for( int i = 0;i < 4;i ++ )
    {
    p1[i] = i;
    } for( int i = 0;i < 4;i ++ )
    {
    CString strMsg;
    strMsg.Format("index %d=%d", i, p1[i]);
    //AfxMessageBox(strMsg);
    } return p2;
    }C#中调用:[DllImport("Test.dll")]
    extern static int func(int[] p1, int p2);private void button1_Click(object sender, System.EventArgs e)
    {
    //TestMessageBox("aaa"); int[] p1 = new int[4]; func(p1, 0);
    for(int i =0;i<p1.Length;i ++)
    {
    MessageBox.Show(p1[i].ToString());
    }
    }