2010-05-19 5 views
0

I 다음 C++ 구조체 가지고"불일치가 발생했습니다"

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

가 I이 구조체의 인스턴스를 취하는 DLL 방식이있다. ,

[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.

을 제공 : 여기가 C#을 측면에서 시도한 무엇인가?

답변

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; 
    } 
관련 문제