用C++写的DLL,
其函数原型为:char **func(){........}(注:返回值为两个字符串,如:the oid=1.3.6.1.2.1.5.0;the value=lenovo)
用C#对其调用并显示结果,其代码如下:
public class RefCommon
{
[DllImport("DLL6.dll",
EntryPoint = "func",
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern char**func(); //此处提示出错(在C#中没有指针,应该如何修改呢?)
public static string invoke() //得到DLL中函数的结果这样写对吗?如何修改??
{
string[] a = new string[2];
a = func();
return a;
}
}
其函数原型为:char **func(){........}(注:返回值为两个字符串,如:the oid=1.3.6.1.2.1.5.0;the value=lenovo)
用C#对其调用并显示结果,其代码如下:
public class RefCommon
{
[DllImport("DLL6.dll",
EntryPoint = "func",
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern char**func(); //此处提示出错(在C#中没有指针,应该如何修改呢?)
public static string invoke() //得到DLL中函数的结果这样写对吗?如何修改??
{
string[] a = new string[2];
a = func();
return a;
}
}
unsafe{
public static extern char**func();
}------------------------------------------
http://www.shenjk.com
public static extern char**func();
}
----------------------------------------------
http://www.shenjk.com
public static extern char**func();
}
----------------------------------------------
http://www.shenjk.com
[DllImport("DLL6.dll",
EntryPoint = "func",
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr func();
取指针里的数据参考Marshal类。
EntryPoint = "func",
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr func();
代码如下:
[DllImport("get_snmp_dll.dll",
EntryPoint = "func",
CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr func();
public static IntPtr invoke() //此处该如何写?(这是我写的)
{
IntPtr a;
a = func();
return a; }
在界面显示时,代码如下:
protected void Page_Load(object sender, EventArgs e)
{
string []x=new string [2];
IntPtr y = Class2 .invoke();
x = y; //提示出错!!
Response.Write("the value is:{0},the oid is:{1}", x[0], x[1]); }
protected void Page_Load(object sender, EventArgs e)
{
string []x=new string [2];
IntPtr y = Class2 .invoke();
x = y; //提示出错!!
Response.Write("the value is:{0},the oid is:{1}", x[0], x[1]); }
y是个指针的指针,也就是y指向的还是个指针p,假设P指向的是个字符串,这时你应这样,
p=Marshal.ReadIntPtr(y);
string text2 = Marshal.PtrToStringAuto(p);
http://topic.csdn.net/u/20080616/09/56f758f7-78a3-4a5c-8bad-903a5deb0acb.html?2140750326
http://topic.csdn.net/u/20080620/12/a63a58e7-7920-41bd-8857-38babf0c1437.html