C # ToCharArray not working with char *

I have the following structure:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
unsafe public struct Attributes
{

    public OrderCommand Command { get; set; }

    public int RefID { get; set; }

    public fixed char MarketSymbol[30];
}

Now I want to write symbols in the MarketSymbol field:

string symbol = "test";
Attributes.MarketSymbol = symbol.ToCharArray();

The compiler throws an error saying that it cannot convert from char [] to char *. How do I write this? Thanks

+5
source share
1 answer

Like this:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public struct Attributes
{
    public OrderCommand Command { get; set; }
    public int RefID { get; set; }
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
    public string MarketSymbol;
}

Beware of pack = 1, this is rather unusual. And a good chance for CharSet.Ansi if it intercepts C code.

+3
source

All Articles