marc29
marc29

Reputation: 21

unsigned char array from c++ in c#

I'm trying to call a C++ dll function in C#. The function has a struct as a parameter and I have a problem with the variable aby_data[8]. I defined the unsigned char[] as a byte[], the program doesn't crash but I don't receive any data.

Struct C++

typedef struct st_canmsg
{
    long l_id;
    unsigned char by_len;
    unsigned char by_msg_lost;
    unsigned char by_extended;
    unsigned char by_remote;
    unsigned char aby_data[8];
    unsigned long ul_tstamp;
} CMSG;

Conversion to C#

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CMSG
{
    public Int32 l_id;
    public byte by_len;
    public byte by_msg_lost;
    public byte by_extended;
    public byte by_remote;
    public byte aby_data[];
    public UInt32 ul_tstamp;
};

CMSG t_CANMsg[] = new CMSG[1];
t_CANMsg[0].aby_data = new byte[8];
t_CANMsg[0].aby_data[0] = 01;
t_CANMsg[0].aby_data[1] = 02;
t_CANMsg[0].aby_data[2] = 03;
t_CANMsg[0].aby_data[3] = 04;
t_CANMsg[0].aby_data[4] = 05;
t_CANMsg[0].aby_data[5] = 06;
t_CANMsg[0].aby_data[6] = 07;
t_CANMsg[0].aby_data[7] = 08;

l_retval = canSend(handle, out t_CANMsg[0], out len);

Upvotes: 0

Views: 1447

Answers (2)

marc29
marc29

Reputation: 21

Based on the Frederik Carlier code I got this solution for use it as array:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CMSG
{
    public Int32 l_id;
    public byte by_len;
    public byte by_msg_lost;
    public byte by_extended;
    public byte by_remote;
    public byte aby_data_0;
    public byte aby_data_1;
    public byte aby_data_2;
    public byte aby_data_3;
    public byte aby_data_4;
    public byte aby_data_5;
    public byte aby_data_6;
    public byte aby_data_7;
    public UInt32 ul_tstamp;

    public byte[] aby_data(){
        byte[] aby_data_array = new byte[8];
        aby_data_array[0] = aby_data_0;
        aby_data_array[1] = aby_data_1;
        aby_data_array[2] = aby_data_2;
        aby_data_array[3] = aby_data_3;
        aby_data_array[4] = aby_data_4;
        aby_data_array[5] = aby_data_5;
        aby_data_array[6] = aby_data_6;
        aby_data_array[7] = aby_data_7;
        return aby_data_array;
    }
};


l_retval = canRead(handle, out t_CANMsg[0], out len);
byte data [] = t_CANMsg[0].aby_data();

Upvotes: 0

Frederik Carlier
Frederik Carlier

Reputation: 4786

You're marshalling a fixed-length array, but you did not specify the size of your array in your struct definition.

Can you try to update your struct definition like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CMSG
{
    public Int32 l_id;
    public byte by_len;
    public byte by_msg_lost;
    public byte by_extended;
    public byte by_remote;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public byte aby_data[];
    public UInt32 ul_tstamp;
};

Alternatively, you can also try to explicitly define the bytes:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CMSG
{
    public Int32 l_id;
    public byte by_len;
    public byte by_msg_lost;
    public byte by_extended;
    public byte by_remote;
    public byte aby_data_0;
    public byte aby_data_1;
    public byte aby_data_2;
    public byte aby_data_3;
    public byte aby_data_4;
    public byte aby_data_5;
    public byte aby_data_6;
    public byte aby_data_7;
    public UInt32 ul_tstamp;
};

If you're still having issues, you also share the declaration of canSend?

Upvotes: 1

Related Questions