C# 是否在互操作边界上复制数据? 假设我有C++写的函数,签名为/p> extern "C" DLL_EXPORT bool __stdcall SendBytes(unsigned char* bytes, int size);
DllImport声明如下:C# 是否在互操作边界上复制数据? 假设我有C++写的函数,签名为/p> extern "C" DLL_EXPORT bool __stdcall SendBytes(unsigned char* bytes, int size);,c#,c++,interop,C#,C++,Interop,DllImport声明如下: [DllImport("MyDLL.dll", CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.I1)] public static extern bool SendBytes(byte[] bytes, int size); 如果我从C打电话# 在进入C++ sEndoByt函数之前,在边界上复制了ByTestOsTeX?<
[DllImport("MyDLL.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool SendBytes(byte[] bytes, int size);
如果我从C打电话#
在进入C++ sEndoByt函数之前,在边界上复制了ByTestOsTeX?< /P>
同样的问题也适用于从函数返回字节的情况
字节类型是BLITHT,但在C++中,将字节数组的指针作为指针指向无符号字符数组是否正确?
UPD:根据注释,字符串从托管内存传递到非托管内存时总是被复制。那么,传递字节的最佳方式是什么?这很复杂。但有很好的记录。我们无法看到您使用的[DllImport]声明。往往当C++代码没有希望正确地执行此操作时,它不知道数组中有多少字节。但通常情况下,不需要,字节[]是一种可blittable类型,pinvoke封送器只需临时固定数组并将指针传递给第一个数组元素。
var bytesToSend = new byte[1000];
// Fill bytesToSend here
// ...
MyAPI.SendBytes(bytesToSend, 1000)