Converting a C++ struct to an C# struct

Sep 29 2009 3:11 PM
Hello,
I am new to C# and have a problem implementing an unmanaged .dll into an C# project which needs managed code.

The .dll is already accepted and also the correct entry points are found, the last problem seems to been the conversion from an C++ struct to an C# struct.

Here is the C++ code:

typedef struct SymbolRequest {
    WORD    SecurityType;
    BYTE    ExchangeId;
    char    Symbol[20];
    DWORD   Flags;
} SymbolRequest;

Of course I have already tried it by my own for several days but it always throws an exception stating that something tries to write or read in secured memory --- I expect that my conversions are wrong. See what I got in C#:

     [StructLayout(LayoutKind.Sequential)]
    public struct SymbolRequestStruct
    {
        public short SecurityType;
        public byte ExchangeId;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
        public string Symbol;
        public uint Flags;
    }

Something has to be wrong and it would be nice if someone can help me since I am having the problem since more than three days.

Thank you in advance.


JB

Answers (1)