[C to C#] Send Structur with byte array from C# to C

hi,

i have a structure :
-------------------------------------------------------------------------------
[StructLayout(LayoutKind.Sequential)]
public struct DRVBINFO
{
public UInt16 wCardsNumber;
public UInt16 wChannelsNumber;
[MarshalAs(UnmanagedType.LPArray)]
public byte[] myIntArray ;
}
-------------------------------------------------------------------------------

i want to send this structure from a C# to a C function
-------------------------------------------------------------------------------
[DllImport("LightMng.dll", SetLastError = true)]
static extern uint
LightMngInit([MarshalAs(UnmanagedType.Struct)]DRVBINFO DRVBinfo);
-------------------------------------------------------------------------------

I use this function
-------------------------------------------------------------------------------
public uint OpenDevice()
{
DRVBINFO DRVBinfo;
DRVBinfo = new DRVBINFO();

DRVBinfo.wCardsNumber = 3;
DRVBinfo.wChannelsNumber = 12;
DRVBinfo.myIntArray = new Byte[3];
DRVBinfo.myIntArray[0] = 0x00;
DRVBinfo.myIntArray[1] = 0x51;
DRVBinfo.myIntArray[2] = 0x00;
uint Return;

Return = LightMngInit(DRVBinfo);
Return = 1;
if (Return == 0) return 0;

return 1;
}
-------------------------------------------------------------------------------

when i use this, i get an exception : "NotSupportedException"

regards,

Julien



Answer this question

[C to C#] Send Structur with byte array from C# to C

  • Ben Weber

    thank you for you answer but i find an other solution that works.

    the struct :

    --------------------------------------------------------------------

    [StructLayout(LayoutKind.Sequential)]

    public struct DRVBINFO

    {

    public UInt16 wCardsNumber;

    public UInt16 wChannelsNumber;

    public IntPtr DRVBCardsAdresses;

    }

    ----------------------------------------------------------------------------------

    [DllImport("LightMng.dll", SetLastError = true)]

    static extern uint LightMngInit (DRVBINFO DRVBinfo);

    -----------------------------------------------------------------------------------

    public uint OpenDevice()

    {

    DRVBINFO DRVBinfo;

    DRVBinfo = new DRVBINFO();

    DRVBinfo.wCardsNumber = 3;

    DRVBinfo.wChannelsNumber = 12;

    DRVBinfo.DRVBCardsAdresses = Marshal.AllocCoTaskMem(3);

    Marshal.WriteByte(DRVBinfo.DRVBCardsAdresses, 0,0x00);

    Marshal.WriteByte(DRVBinfo.DRVBCardsAdresses, 1, 0x51);

    Marshal.WriteByte(DRVBinfo.DRVBCardsAdresses, 2, 0x00);

    uint Return;

    Return = LightMngInit(DRVBinfo);

    if (Return == 0) return 0;

    return 1;

    }

    --------------------------------------------------------------------------------------------------------------------


  • foxman_

    HtH,

    i was trying your solution and i cannot get it to work i get this error:

    "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    how do you actualy use the array

    [StructLayout(LayoutKind.Sequential)]
    public struct
    DRVBINFO
    {
    public UInt16 wCardsNumber;
    public UInt16 wChannelsNumber;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
    public byte[] myIntArray;
    }

    do you do this

    DRVBINFO tst = new DRVBINFO();

    tst.myIntArray = new byte[3] // beacuse this kind of use gives me that error or is there another way


  • brian_tsim

    Hello All.

    Julien:

    Well, alrighty then. Good luck with that memory leak.

    HTH.



  • Hans1982

    Hello All.

    Julien:

    Try this instead:

    [StructLayout(LayoutKind.Sequential)]
    public struct DRVBINFO
    {
    public UInt16 wCardsNumber;
    public UInt16 wChannelsNumber;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
    public byte[] myIntArray;
    }

    HTH.



  • [C to C#] Send Structur with byte array from C# to C