unsafe class MyDll
{
    [System.Runtime.InteropServices.DllImport("Comm.dll")]
    public static extern fun(byte* x);
}...
unsafe
{
    ...
    byte* buf = stackalloc byte[100];
    MyDll.fun(buf);
    byte[] r = /* buf[6] ~ buf[17] 是我想要的数据,怎么弄出来? */
    ...
}

解决方案 »

  1.   

    猜想的拷贝代码:byte[] r = new byte[12]; //这个就在堆上for(int i=0;i<r.Length;i++)
    {
       r[i]=buf[6+i];
    }
      

  2.   

    其实可以直接这样:
    public static extern fun(byte[] x);
      

  3.   

    正如3楼所说,既然都不是同一块内存了,不拷贝还能咋的?不过可以用Marshal.Copy方法来简化拷贝的代码
      

  4.   


    unsafe
    {
        byte[] buffer = new byte[100];
        fixed (byte* buf = buffer)
        {
            MyDll.fun(buf);
        }
        //buffer就是想要的数据 
    }
      

  5.   


    class MyDll
    {
        [System.Runtime.InteropServices.DllImport("Comm.dll")]
        public static extern fun(byte[] x);
    }...    ...
        byte[] buf = newc byte[100];
        MyDll.fun(buf);
        用buf吧
      

  6.   

    就是说c#里的方法的参数声明成byte[]的
      

  7.   

    真的不建议用unsafe,你这个直接可以用ref byte[]替代
      

  8.   

    你就可以直接使用那个byte[]了,你的问题就不存在了