How to pass C# string[] to C char** or char[][]

Viewed 47

in C/C++ I have

typedef char TDAStringType[256];

struct CMarketReqMarketDataField
{
    TDACharType         MarketType;                     
    TDACharType         SubscMode;                      
    TDAIntType          MarketCount;                    
    TDAStringType       MarketTrcode[20];   
    TDAStringType       ErrorDescription;               
}

Look at the member MarketTrcode, It seems stock an array of string/char *, but I need to pass values to this struct in C/C++ through C# string[] frontend, How can I make it? using SWIG 4.0.2

1 Answers

The equivalent C# structs for PInvoke marshalling would be

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct TDAStringType
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string String;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct CMarketReqMarketDataField
{
    public char MarketType;                     
    public char SubscMode;                      
    public int MarketCount;                    
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public TDAStringType[] MarketTrcode;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string ErrorDescription;
}
Related