2010-05-19 2 views
0

У меня следующая структура C++:сортировочного массив шорт: «Несовпадение произошло»

typedef struct FormulaSyntax{ 
     WORD StructSize; 
     short formulaSyntax [2]; 
    } FormulaSyntax; 

У меня есть метод, который принимает DLL экземпляр этой структуры. Вот что я пытался на стороне C#:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
    public struct FormulaSyntax { 
     public short StructSize; 
     public short[] formulaSyntax; 
    } 

    [DllImport(DLL_NAME, EntryPoint = "PEGetFormulaSyntax", 
        CharSet = CharSet.Unicode)] 
    public static extern bool getFormulaSyntax(short reportID, 
        ref FormulaSyntax syntax); 

    ... 
    FormulaSyntax syntax = new FormulaSyntax(); 
    syntax.formulaSyntax = new short[2]; 
    syntax.StructSize = (short)Marshal.SizeOf(syntax); 
    PrintEngine.getFormulaSyntax(context.oldApiID, ref syntax); 

Это происходит сбой, давая мне сообщение

Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata.

Что я делаю неправильно?

ответ

0

Обнаружили ответ here: Вот что должно было выглядеть для моей C# структуры - нужна линия MarshalAs. Он работает сейчас.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
    public struct FormulaSyntax { 
     public short StructSize; 
     [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4, 
              SizeConst = 2)] 
     public short[] formulaSyntax; 
    } 
Смежные вопросы