dll是C++写的,函数原型是int func(unsigned char* a, unsigned char* b){...},我在C#里调用时想通过dll生成一个Byte型数组,如byte[] by=new byte[300],我的调用是这么写的://声明函数
[DllImport.......]
 static extern int func(ref byte b, ref byte a);byte by1=new by1[300];
byte by2=new by2[300];func(ref by1[0], ref by2[0])
运行的时候出了异常:
[System.AccessViolationException] = {"尝试读取或写入受保护的内存。这通常指示其他内存已损坏不知道我们这么调用对不对,请大侠指教

解决方案 »

  1.   

    char*  是字符串 对应的应该是STRING,或者byte[]
    byte 是一个字节
      

  2.   

    [DllImport.......]
    static extern int func(byte[] b, byte[] a);byte[] by1=new by1[300];
    byte[] by2=new by2[300];func(by1, by2)
      

  3.   

    http://topic.csdn.net/t/20040816/11/3278042.html
    这个帖子讨论的比较详细,你可以看看
      

  4.   

    char   *对应的C#类型
    [MarshalAs(UnmanagedType.LPTStr)]
    StringBuilder   sMsgID;注意你分配的长度,char* 是有长度的,一般C++函数会同时提供Char* 指针和他的长度
    如果长度不正确一样会出错
      

  5.   

    http://topic.csdn.net/u/20101125/11/dca2ab2b-3c67-4d34-bd78-e6f23ea0ee99.html
      

  6.   

    char* 直接string 就可以了