2010-05-19 4 views
0

I ont les suivantes struct C++:sérialisation un ensemble de courts métrages: "Mismatch est produite"

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

I ont une méthode DLL qui prend une instance de cette structure. Voici ce que j'ai essayé sur le côté 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); 

Ce plante, me donner le message

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

Qu'est-ce que je fais mal?

Répondre

0

J'ai trouvé la réponse here: Voici à quoi ma structure C# devait ressembler - elle avait besoin de la ligne MarshalAs. Ça fonctionne maintenant.

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
    public struct FormulaSyntax { 
     public short StructSize; 
     [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4, 
              SizeConst = 2)] 
     public short[] formulaSyntax; 
    } 
Questions connexes