从C ++中的char *获取C#中的byte [] [英] Get byte[] in C# from char* in C++
问题描述
在C#中,我有一个数据类型byte[]
,我想使用返回char*
In C# I have an data type byte[]
, which I want to fill in using a C++ function which returns char*
C ++函数(在ImageData.dll
中)
The C++ function (in ImageData.dll
)
char* pMemoryBuffer = NULL;
char* LoadData(const char *fileName)
{
// processing pMemoryBuffer ...
return pMemoryBuffer;
}
将本机dll导入C#:
Import native dll into C#:
[DllImport(".\\Modules_Native\\ImageData.dll", EntryPoint = "LoadData")]
private extern static byte[] LoadData(string fileName);
C#中的byte[]
数据
byte[] buffer = new byte[256*256];
buffer = LoadData("D:\\myPic.tif");
显然它还没有工作,但是它提出了我想做什么的想法.所以我想知道如何使这项工作,以及正确的方法是什么.非常感谢您的教育.
Apparently it is not working yet, but it presents the idea of what I want to do. So I am wondering how to make this work, and what is the right way to do it. Thanks very much for your education.
推荐答案
尝试一下
// c++
void LoadData(unsigned char* *pMemoryBuffer, const char *fileName)
{
// processing pMemoryBuffer ...
*pMemoryBuffer = resutss;
}
将本机dll导入C#:
Import native dll into C#:
[DllImport(".\\Modules_Native\\ImageData.dll", EntryPoint = "LoadData")]
private extern static void LoadData(out IntPtr data, string fileName);
当函数返回数据时,它将指向数组,您可以使用Marshal类读取内容.我想您会将其复制到新的字节arra y.>
When the function returns data will point to the array and you can read the contents using the Marshal class. I guess you would copy it to a new byte array.
byte[] buffer = new byte[256*256];
buffer = Marshal.Copy(LoadData(buffer ,"D:\\myPic.tif"), buffer , 0, buffer.Length);
这篇关于从C ++中的char *获取C#中的byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!