gpt4 book ai didi

c# - 使用 DllImport 将 C# byte[] 编码到 C++ DLL

转载 作者:行者123 更新时间:2023-12-05 08:04:53 27 4
gpt4 key购买 nike

在将 C# byte[] 数组编码为 C++ 时,我遇到了一些奇怪的行为。

当 byte[] 作为参数传递时,我得到了 C++ 中的预期数据。 (查看报告数据)

当 byte[] 被包裹在一个结构中时,我得到了奇怪的值。 (参见 ReportBuffer)

是什么导致了这种行为上的差异?由于我需要将数据包装在更复杂的用例中,是否可以纠正它?

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;
}
}
}

控制台输出

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;
}
}
}

关于c# - 使用 DllImport 将 C# byte[] 编码到 C++ DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67125080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com