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

标签 c# c++ marshalling dllimport

在将 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/

相关文章:

c# - 一个好的(最好是免费的).Net 应用程序安装程序?

c++ - 如何从文件中删除记录?

json - 实现实体组件系统的最佳方式是什么

java - 使用 java Marshaller 更改命名空间根名称

c# - 如何使用 C# 显示具有两种状态的数据

c# - 使用 DbContext 接口(interface)注册 AspNetCore 2.1 Identity 系统

c# - 如何获取类的枚举属性列表?

c++ - 多线程应用程序中的槽执行策略

c++ - strcmp() 可以比较动态字符数组和静态字符数组吗?

spring-boot - 编码错误 - 缺少 xmlrootelement 注释错误