小弟是菜鸟, 在 调用 c++的dll 的出了一个问题。
c++ dll的函数为     int  GetIDList(HANDLE khandle,char IDList[][20] , int &IDNum)   
我封装的c#函数为  public static extern int GetIDList(IntPtr khandle, char[][] IDList, ref int IDNum);   
我声明了一个数组  public  char[][]  ID_st;              
            ID_st = new char[10000][];
            for (int i = 0; i < 10000;i++ )
            {
                ID_st[i]=new char[20];
            }
但是在调用该函数后发生异常 ,说  运行的时候出异常了  无法封送处理“parameter #2”: 嵌套的数组不支持封送处理  然后我换一种封装方式  public static extern int GetIDList(IntPtr khandle, string[] IDList, ref int IDNum);   
  string [] ID_st =new string[10000];  
然后在调用函数后 ,可以运行 但是 string 数组中 都还是空的,没有得到赋值,各个高手 帮我看看 ,该怎样封装啊。

解决方案 »

  1.   

    二级指针啊!  要用IntPtr做参数声明
      

  2.   

     我说得应该挺清楚了吧啊, 我调用这个函数时  int result = GetIDList(khandle,ID_st,ref num); 
      

  3.   

    //int  GetIDList(HANDLE khandle,char IDList[][20] , int &IDNum)  
    试试这样
    public static extern int GetIDList(IntPtr khandle, IntPtr IDList, ref int IDNum);//..........
    byte [] Buffer=new Byte[4096];unsafe{
     fixed(byte* pb=&Buffer[0]){
       IntPtr p=(IntPtr)pb;
       GetIDList(kHandle,p,ref IDNum); }
    }
      然后再 Buffer里面,每20个字节是一段
      

  4.   

    第二种方式下,你把ID_st〔i〕都赋值一下不就有值了吗?不大懂你的意思。
      

  5.   

    二级指针啊!  要用IntPtr做参数声明
      

  6.   

    可以参考一下:
    http://blog.csdn.net/Mittermeyer/archive/2007/04/27/1586867.aspx建议申明1维数组,返回的数据然后自己处理,例如包装个类,定义个方法,按照[][]的形式访问。
      

  7.   

    回复 7 楼 :
      大哥 我第二种封装方式下,ID_st 是调用c++的dll来赋值的,关键就是c++的dll没有给我赋值成功啊?!
      

  8.   

    不能用String[],String[]并不等于char[][],interop不会帮你做这种转换的,只能定义为char[],或者用intptr,申请一块内存,调用API之后自己解析按照约定规则解析。
      

  9.   

    public fixed byte name[100];

    public byte* name=Statalloc byte[100];
      

  10.   

    不大明白
    要想返回值参数是不是得声明为ref
      

  11.   


    string [] ID_st =new string[10000];  
    public static extern int GetIDList(IntPtr khandle, ref string[] IDList, ref int IDNum); 
      

  12.   

    因为string是一种特殊的引用类型,所以需要加上ref 关键字
      

  13.   

    呵呵.改为.char[][20] IDList 试下看!
      

  14.   

    1,将C++的函数原型改成 int  GetIDList(HANDLE khandle,VARIANT IDList, int &IDNum) 并修改相应的C++内部代码;
    2,将C#的声明改成public static extern int GetIDList(IntPtr khandle, object IDList, ref int IDNum);  
    3,在c#中声明一个char[][]型的数组,赋好值,传入函数试试.