日期:2014-05-18  浏览次数:20998 次

C#调用dll,这个怎么传进去?
int GetListName(char** pList[])
{
}

请问C#要调用这个函数,怎么声明变量?\
高手来啊

------解决方案--------------------
string大多数情况下是不能传的.因为是不可变的.通常都是stringbuilder
------解决方案--------------------
The first question is: do you really need it?
It is a highly uncommon practice passing an array of "pointer to string pointer".

The second question is: who is responsible for allocating and freeing the memory?
In your case,
The callee doesn't know when it is safe to free the memory,
The caller doesn't know how large a storage it should prepare to call that function.

If you cannot answer these questions, the most likely result will be either memory corruption or memory leak.




------解决方案--------------------
[DllImport("xxx.dll")]
private static extern int GetListName(char** pList[])
------解决方案--------------------
如果用unsafe呢?
------解决方案--------------------
[DllImport("xxx.dll")] 
private static extern int GetListName(out string pList)
------解决方案--------------------
参数为输出参数时,一般用StringBuilder.
[DllImport...]
public int GetListName( [Out]IntPtr[] pList); 
IntPtr[] PtrList = new IntPtr[5]; 
for(int i = 0; i < 5; ++i) 

PtrList[i] = Marshal.StringToCoTaskMemAuto(new string(100)); 

GetListName(PtrList);
然后用Marshal.PtrToStringAnsi或者Marshal.PtrToStringAuto来取得string. 形如:
string sss=Marshal.PtrToStringAnsi(PtrList[0]);//for(0-5)
//string sss=Marshal.PtrToStringAuto(PtrList[0]); //for(0-5)

最后记得用Marshal.FreeCoTaskMem释放内存!!
------解决方案--------------------
试试这个
C# code

  [DllImport(@"DllA.dll")]
  public static extern  int GetListName(ref char[] k);
  public void DLLCall(object sender, EventArgs e)
  {
    char[] k = { '6', '8', '9' };
    int i = GetListName(ref k);
    Response.Write(i.ToString());
  }