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

怎么调用vc写的dlll直接返回指针数组

2013-08-01 
如何调用vc写的dlll直接返回指针数组如提vc dll原函数为extern C unsigned char * ReadHid(void) {unsig

如何调用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);


 MessageBox.Show(ss);


明白了不怎么调用vc写的dlll直接返回指针数组

看错了,你那个函数是没参数的以为是char* ReadHid(char* string),没参数的话就直接是:

 IntPtr intptr = ReadHid();
 string ss = Marshal.PtrToStringAnsi(intptr);
 MessageBox.Show(ss);

[解决办法]
引用:
在类前面加上unsafe是可以了,
byte* pb = (byte*)ReadHid();
但从这个unsafe单词里看是不安全的。

因为要写一个c#的demo给客户调用dll。能否就以我这个dll原函数,高手能否帮忙写个以IntPtr调用我这个函数的例子呢

其实unsafe代码也没啥不安全的,只是用了指针而已。
如果不想直接用,可以private extern static IntPtr ReadHid();

IntPtr ptr=ReadHid();
然后
转字符串:Marshal.PtrToStringXXX(ptr)
读整数:Marshal.ReadInt32(ptr,0);  第二个参数是偏移
转结构:Marshal.PtrToStructure

热点排行