首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#调用dll,这个如何传进去

2012-01-20 
C#调用dll,这个怎么传进去?int GetListName(char** pList[]){}请问C#要调用这个函数,怎么声明变量?\高手来

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());  } 

热点排行