Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否在互操作边界上复制数据? 假设我有C++写的函数,签名为/p> extern "C" DLL_EXPORT bool __stdcall SendBytes(unsigned char* bytes, int size);_C#_C++_Interop - Fatal编程技术网

C# 是否在互操作边界上复制数据? 假设我有C++写的函数,签名为/p> extern "C" DLL_EXPORT bool __stdcall SendBytes(unsigned char* bytes, int size);

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声明如下:

    [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)