由 DLL 5 如何創建 C# 可以使用的 C++ DLL檔案 中的Array轉移方式
C++ 的轉移方式
extern "C" Array_API unsigned char* C_Array(int index);
unsigned char* Array_in;
extern unsigned char* C_Array(int index)
{
index = index * sizeof(unsigned char);
unsigned char* Arry_out = (unsigned char*)malloc(index);
memcpy(Arry_out , Array_in , index);
return Arry_out ;
}
C# 的接收Array (PInvoke)
[DllImport("CtoCsharp_Array.dll", EntryPoint = "C_Array", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr C_Array(int index);
public bool Array_Capture( ref byte[] Csharp_Buffer,int index)
{
Marshal.Copy(C_Array(index), Csharp_Buffer, 0, index);
}