c++ DLL 返回一个指针,C#怎么按照长度 指针 取出非托管内存的值 并转换为字符串

c++ DLL 返回一个指针,C#怎么按照长度 指针 取出非托管内存的值 并转换为字符串

c++
char *func(unsigned char buf, int buflen);
对应的c#
byte[] func(byte[] buf, int buflen);
然后byte[] str = new byte[xxx];
str = func(buf, buflen);
---------------------------------------------
c# string和byte[]转换
byte[] byteArray = System.Text.Encoding.Default.GetBytes( str );

反过来也是一样,把byte[]转成string:

string str = System.Text.Encoding.Default.GetString( byteArray );

其实,在System.Text.Encoding class中,还有很多其它有用的方法,像GetChars,从一个byte[]转成一个char[],等等,可以参考MSDN。
另外,还有其它编码方式的,如System.Text.UTF8Encoding class、System.Text.UnicodeEncoding class等,根据不同需要可选不同的class。追问

我现在已经得到了byte流 转换为Image之后 直接报错。。 是不是流不对

追答

转换为Image 是什么意思? 报什么错?
请把c++的原型和你要做的操作说明下, 这样才能理解你的意思,做更好的解答

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-07-28
lpstr这类指针在C#里用StringBuilder代替,不需要什么根据长度,直接用就行了。追问

我现在已经得到了byte流 转换为Image之后 直接报错。。 是不是流不对

第2个回答  2012-07-27
char* 的话你可以用byte[]来接收
得到byte[]再得到字符串就简单了追问

我现在已经得到了byte流 转换为Image之后 直接报错。。 是不是流不对