日期:2014-05-17  浏览次数:20922 次

如何调用vc写的dlll直接返回指针数组
如提
vc dll原函数为

extern "C" unsigned char * ReadHid(void) 
{
unsigned char * r;
//……
return r;
}


------解决方案--------------------
IntPtr,然后自己去转
------解决方案--------------------
ref byte 当成数组处理,网上有很多。
这种类型还不算烦的。
------解决方案--------------------
不需要开启不安全代码, *char 用 intPrt或者sting接收返回值都可以
------解决方案--------------------
引用:
我已做成这样

private extern unsafe static byte* ReadHid();  

之后就不会了
能不能详细点(完整点)的的例子


byte* pb=ReadHid();
然后就随便你处理了
字节数组:byte b=*pb; pb++;
字符串:string str=Marshal.PtrToString(new IntPtr((void*)pb);
整数数组:int i=*(int*)pb; pb++;
结构数组:都可以
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

我已做成这样

private extern unsafe static byte* ReadHid();  

之后就不会了
能不能详细点(完整点)的的例子


byte* pb=ReadHid();
然后就随便你处理了
字节数组:byte b=*pb; pb++;
字符串:string str=Marshal.PtrToString(new IntPtr((void*)pb);
整数数组:int i=*(int*)pb; pb++;
结构数组:都可以


还是有点问题
问题一:如果使用private extern unsafe static byte* ReadHid();
指针和固定大小缓冲区只能在不安全的上下文中使用


这个你在调用的地方也要unsafe里,你干脆在类前面加上unsafe
------解决方案--------------------
引用:
我去...有现成的呀

 StringBuilder sbuilder= new StringBuilder();
 IntPtr intptr = ReadHid(sbuilder);
 string ss = Marshal.PtrToStringAnsi(intptr);