Marshalling nested struct from C# to C++ DLL

1

I am trying to call a function from C# to a .DLL written in Borland C++ whose signature is:

extern "C" __declspec(dllexport) ls50errortype __stdcall Ls50P2Open(ls50p2apiconfiginfostruct &configinfo);

The corresponding call in C#:

[DllImport("C:\\Lumistar\\LDPS_8x\\Ls50P2_Dll.dll", EntryPoint = "Ls50P2Open", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern void Ls50P2Open(ref ls50p2apiconfiginfostruct configinfo);

The structure of interest (ls50p2apiconfiginfostruct) is composed of nested structures and enums (copied from the C++ header):

typedef enum
{
    MFMODEL_LS50P1,
    MFMODEL_4422PCI,
    MFMODEL_LS50P2,
    MFMODEL_LS70P2,
    MFMODEL_LS5070,
    MFMODEL_LAST
}ecardmodel;    

typedef enum
{
    CHANNELDEVICE_NONE,
    CHANNELDEVICE_50,
    CHANNELDEVICE_70,
    CHANNELDEVICE_LAST
}ls50p2channeldevicetype;

typedef enum
{
    LS50V2DCARD_NONE, 
    LS50V2DCARD_40V1_10,
    LS50V2DCARD_40V1_20,
    LS50V2DCARD_40V2_10,
    LS50V2DCARD_40V2_20,
    LS50V2DCARD_38,
    LS50V2DCARD_LAST
}ls50p2daughtercardtype;


typedef struct
{
    bool HasDaughterCard;
    ls50p2daughtercardtype DCardType;
    bool SpecialStatusCapable;

    int MaxBitsyncInputs;
    bool HasBitsyncConfidenceLevel;

    bool HasBitsync2ndCh;
    bool SpecialStatusCapable2ndCh;
    bool HasBitsyncConfidenceLevel2ndCh;

    ls50p2daughtercardtype DCardType2ndCh;
    int MaxBitsyncInputs2ndCh;
}ls50p2daughtercardinfostruct;

typedef struct
{
    ecardmodel DeviceModel;
    ls50p2channeldevicetype ChannelDataTypeAry[2];
    ls50p2daughtercardinfostruct DaughterCardInfo;

    bool HasExtendedBertPatterns;

    int FirmwareVersionAry[2];
    int NumPremodFiltersAry[2];
    double Ls50SimPreModFilterKhzAry[2][LS50V2_MAX50SIMPREMODFILTERS];
    double Ls50SimMinFmDeviationKhzAry[2];
    double Ls50SimMaxFmDeviationKhzAry[2];
}ls50p2cardconfigstruct;

typedef struct
{
    unsigned char *DataBuf;
    HANDLE hNewDataRdy;
    DWORD MaxBufLength;
    DWORD CurrentBufLength;
    int NumHeaderBytes;
}ls50p2carddatastruct;

typedef struct
{
    ls50p2cardconfigstruct CardConfigInfo[MAXMFCARDS];
    int Ls50P2CardCount;
    ls50p2carddatastruct DataInfo[MAXMFCARDS][2];
}ls50p2apiconfiginfostruct;

Here's the corresponding struct in C#:

    public enum ecardmodel
    {
        MFMODEL_LS50P1,
        MFMODEL_4422PCI,
        MFMODEL_LS50P2,
        MFMODEL_LS70P2,
        MFMODEL_LS5070,
        MFMODEL_LAST
    }

    public enum ls50p2channeldevicetype
    {
        CHANNELDEVICE_NONE,
        CHANNELDEVICE_50,
        CHANNELDEVICE_70,
        CHANNELDEVICE_LAST
    };

    public enum ls50p2daughtercardtype
    {
        LS50V2DCARD_NONE,
        LS50V2DCARD_40V1_10,
        LS50V2DCARD_40V1_20,
        LS50V2DCARD_40V2_10,
        LS50V2DCARD_40V2_20,
        LS50V2DCARD_38,
        LS50V2DCARD_LAST
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ls50p2daughtercardinfostruct
    {
        public bool HasDaughterCard;
        public ls50p2daughtercardtype DCardType;
        public bool SpecialStatusCapable;

        public int MaxBitsyncInputs;
        public bool HasBitsyncConfidenceLevel;

        public bool HasBitsync2ndCh;
        public bool SpecialStatusCapable2ndCh;
        public bool HasBitsyncConfidenceLevel2ndCh;

        public ls50p2daughtercardtype DCardType2ndCh;
        public int MaxBitsyncInputs2ndCh;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ls50p2cardconfigstruct
    {
        public ecardmodel DeviceModel;
        public ls50p2daughtercardtype[] ChannelDataTypeAry;
        public ls50p2daughtercardinfostruct DaughterCardInfo;

        public bool HasExtendedBertPatterns;

        public int[] FirmwareVersionAry;
        public int[] NumPremodFiltersAry;
        public double[] Ls50SimPreModFilterKhzAry;
        public double[] Ls50SimMinFmDeviationKhzAry;
        public double[] Ls50SimMaxFmDeviationKhzAry;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ls50p2carddatastruct
    {
        public StringBuilder DataBuf;
        public IntPtr hNewDataRdy;
        public uint MaxBufLength;
        public uint CurrentBufLength;
        public int NumHeaderBytes;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ls50p2apiconfiginfostruct
    {
        public ls50p2cardconfigstruct[] CardConfigInfo;
        public int Ls50P2CardCount;
        public ls50p2carddatastruct[,] DataInfo;
    }

Here's the code in C# that I use to call the function:

    ls50p2apiconfiginfostruct lscfg = new ls50p2apiconfiginfostruct();

    lscfg.CardConfigInfo = new ls50p2cardconfigstruct[8];
    for (int i = 0; i < 8; i++)
    {
        lscfg.CardConfigInfo[i].ChannelDataTypeAry = new ls50p2daughtercardtype[2];
    }

    lscfg.DataInfo = new ls50p2carddatastruct[8, 2];

    Ls50P2Open(ref lscfg);

I have tried making this struct in C# but I haven't had much success (problems with enums, 2D arrays, fixed sized buffers). What is the correct way to create this structure in C#? Would this need to be done in an unsafe context?

Now for some reason I get the following error when running the code:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Library.dll

Additional information: Old format or invalid type library. (Exception from HRESULT: 0x80028019 (TYPE_E_UNSUPFORMAT))
c#
c++
struct
enums
marshalling
asked on Stack Overflow Dec 13, 2012 by MSumulong • edited Dec 13, 2012 by MSumulong

2 Answers

2

What does your C# structure looks like. Are you using the StructLayoutAttribute? http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx

You can use it with the option sequential so you just would have to fill your c# structure with fields in the right order.

answered on Stack Overflow Dec 13, 2012 by Florian
1

I believe the array problem is more or less answered here; Improper marshaling: C# array to a C++ unmanaged array

The accepted answer shows how to safely marshal a dynamically allocated array.

As for the enums, they shouldn't pose any problems, there is a clean 1:1 mapping. In fact, I would do it as described in this msdn post; http://blogs.msdn.com/b/abhinaba/archive/2007/08/27/sharing-enums-across-c-and-c.aspx

You can simply define all your enums in a .cs file then include it in both projects and everything will work fine.

answered on Stack Overflow Dec 13, 2012 by evanmcdonnal • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0