从c#中的非托管c++ DLL中获取字节数组上的指针
get pointer on byte array from unmanaged c++ dll in c#
在c++中我有这样的函数
extern "C" _declspec(dllexport) uint8* bufferOperations(uint8* incoming, int size)
我正在尝试从c#中调用它,像这样
[DllImport("MagicLib.DLL", CallingConvention = CallingConvention.Cdecl)]
//[return: MarshalAs(UnmanagedType.ByValArray)]//, ArraySubType=UnmanagedType.SysUInt)]
public static extern byte[] bufferOperations(byte[] incoming, int size);
但是我得到了无法封送"返回值":无效的托管/非托管类型组合
(()问题是-如何正确地编组它?谢谢你阅读我的问题
byte[]是一个已知长度的。net数组类型。你不能封送byte*给它,因为。net不知道输出数组的长度。您应该尝试手动编组。将byte[]替换为byte*。然后,像这样做:
[DllImport("MagicLib.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern byte* bufferOperations(byte* incoming, int size);
public void TestMethod()
{
var incoming = new byte[100];
fixed (byte* inBuf = incoming)
{
byte* outBuf = bufferOperations(inBuf, incoming.Length);
// Assume, that the same buffer is returned, only with data changed.
// Or by any other means, get the real lenght of output buffer (e.g. from library docs, etc).
for (int i = 0; i < incoming.Length; i++)
incoming[i] = outBuf[i];
}
}
在这种情况下不需要使用unsafe contexts
。只要使用IntPtr。
[DllImport("MagicLib.DLL", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr bufferOperations(IntPtr incoming, int size);
然后你可以使用Marshal。复制以从中获取字节数组
相关文章:
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 基于字节数组生成静态范围整数值
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 swig 追加到字节数组
- C++ 替换字节数组中项的顺序
- QDataStream 读取和写入的字节数比 QFile::length() 报告要多
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 将字节数组转换为多维数组
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- C++字节数组到整数
- 将字符串转换为无符号字符数组/字节数组
- 用于 progmem 的C++和头文件压缩的 Web 文件字节数组
- mmap 中的长度是字节数还是页数?
- 如果我向一个12字节的缓冲区写入的字节数少于12,会发生什么情况
- 从 UTF-8 字节数组创建字符串?
- ReadFile() 读取的字节数少于其参数中的指定值