如何在c ++ dll中调用函数时将bytearray返回给c# [英] how to return a bytearray back to c# when a function is called in c++dll

查看:120
本文介绍了如何在c ++ dll中调用函数时将bytearray返回给c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中我使用c ++ dll,它被导入到c#gui中,在函数调用时我希望将字符串返回给c#gui我将函数的返回类型设置为字符串,但它不起作用。我做了什么?

extern_c dll_export std :: string decoding(char filename [])

{

//文件在这里打开;

unsigned char buffer [1024];

length = myfile.tellg();

if(myfile.isOpen())

{

buffer = new char [length];

myfile.read(buffer,length);

返回缓冲区; < br $>
}



其他

return'filedoesn'texist';

}

In my project I use c++ dll, which is imported into c# gui, at the time of function call i want to return string back to c# gui i set return type of function as string but it doesn't work . what i do?
extern_c dll_export std::string decoding(char filename[])
{
//file is opened here;
unsigned char buffer[1024];
length=myfile.tellg();
if(myfile.isOpen())
{
buffer=new char[length];
myfile.read(buffer,length);
return buffer;
}

else
return'filedoesn'texist';
}

推荐答案

见这里: http://stackoverflow.com/questions/370079/pinvoke-for-c-function-that-returns-char [ ^ ] - 如果C ++代码不受管理,它比你想象的要复杂得多。
See here: http://stackoverflow.com/questions/370079/pinvoke-for-c-function-that-returns-char[^] - it's more complex than you might think if the C++ code is unmanaged.


这篇关于如何在c ++ dll中调用函数时将bytearray返回给c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆