简体   繁体   中英

Marshalling C# byte[] to C++ DLL with DllImport

I'm experiencing some strange behaviour when marshalling a C# byte[] array to C++.

When the byte[] is passed as an argument I get the expected data in C++. (See ReportData)

When the byte[] is wrapped in a struct I get strange values out. (See ReportBuffer)

What is causing this difference in behaviour and is there anyway to correct it as I need to have the data wrapped in a more complex use case?

C# Calling Code

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 Code

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

Console Output

Report Buffer
1
0
128
0
1
0


Report Data
0
1
2
3
4
5

I discovered a solution by changing the byte[] array to an IntPtr, allocating the space and copying the data.

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

The C++ code remains the same:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM