1
我输出了一些东西到用c编写的数组,然后我希望从c#调用通过dll获取信息,但是失败了。没有警告,但我可以注意得到正确的信息。测试代码如下:如何从c#通过dll获取c数组?
@ips存储输出信息
UDPDLL_API int get_by_csharp_tst(char *** ips){
char **ip = NULL;
int i = 0;
*ips = (char**)malloc(sizeof(char*)*10);
if(ips == NULL){
perror("overflow");
}
ip = *ips;
for(i =0 ; i <10 ; i++){
*ip = (char*)malloc(16);
memcpy(*ip,"255.255.255.255",16);
*ip++;
}
return 0;
}
调用从C#如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace dll_call
{
class Program
{
[DllImport("udpdll.dll",EntryPoint="get_by_csharp_tst")]
public static extern int get_by_csharp_tst(byte [,] ai);
static void Main(string[] args)
{
int i = 0;
byte[,] ips = new byte[10, 16];
Program.get_by_csharp_tst(ips);
for (i = 0; i < 10; i++) {
Console.WriteLine(ips);
}
Console.Read();
}
}
}
再次感谢您。任何帮助将不胜感激 !
一个指向指针的指针.. yowza。调试时它会返回什么? – 2013-04-10 10:13:16
'[,]'不是你想象的那样 – leppie 2013-04-10 10:16:55
@SimonWhitehead:这将是'ref string []'(或类似的) – leppie 2013-04-10 10:18:02