[英]Marshalling C# byte[] to C++ DLL with DllImport
在將 C# byte[] 數組編組到 C++ 時,我遇到了一些奇怪的行為。
當 byte[] 作為參數傳遞時,我在 C++ 中得到了預期的數據。 (見報告數據)
當 byte[] 被包裝在一個結構中時,我會得到奇怪的值。 (見報告緩沖區)
是什么導致了這種行為差異,並且無論如何都要糾正它,因為我需要將數據包裝在更復雜的用例中?
C# 調用代碼
public struct Buffer
{
public int DataLength;
public byte[] Data;
public Buffer(byte[] data) : this()
{
Data = data;
DataLength = data.Length;
}
}
internal class Program
{
[DllImport(@"C:\Users\lawsm\source\repos\MarshallingTest\Debug\Test.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void ReportBuffer(Buffer buffer);
[DllImport(@"C:\Users\lawsm\source\repos\MarshallingTest\Debug\Test.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void ReportData(byte[] data, int dataCount);
private static void Main(string[] args)
{
byte[] data = new byte[] {0, 1, 2, 3, 4, 5};
Buffer buffer = new Buffer(data);
Console.WriteLine("Report Buffer");
ReportBuffer(buffer);
Console.WriteLine("\n\nReport Data");
ReportData(data, data.Length);
Console.ReadKey();
}
}
C++ Dll 代碼
#include <cstdint>
#include <iostream>
struct Buffer
{
public:
int DataLength;
uint8_t* Data;
};
extern "C"
{
_declspec(dllexport) void ReportBuffer(const Buffer& buffer)
{
for (int i = 0; i < buffer.DataLength; i++)
{
std::cout << (int)buffer.Data[i] << std::endl;
}
}
_declspec(dllexport) void ReportData(uint8_t* data, int dataLength)
{
for (int i = 0; i < dataLength; i++)
{
std::cout << (int)data[i] << std::endl;
}
}
}
控制台 Output
Report Buffer
1
0
128
0
1
0
Report Data
0
1
2
3
4
5
我發現了一個解決方案,將 byte[] 數組更改為 IntPtr,分配空間並復制數據。
[StructLayout(LayoutKind.Sequential)]
public struct Buffer
{
public int DataLength;
public IntPtr Data;
public Buffer(byte[] data) : this()
{
Data = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, Data, data.Length);
DataLength = data.Length;
}
}
[DllImport("Buffer.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void ReportBuffer(Buffer buffer);
C++ 代碼保持不變:
struct Buffer
{
public:
int DataLength;
uint8_t* Data;
};
extern "C"
{
_declspec(dllexport) void ReportBuffer(const Buffer& buffer)
{
std::cout << buffer.DataLength << std::endl;
for (int i = 0; i < buffer.DataLength; i++)
{
std::cout << (int)buffer.Data[i] << std::endl;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.