3

我有一个非托管函数(它将其他三个函数作为参数,这使得这更加令人困惑,因为 VS 没有说明哪个函数是问题所在)。.NET 运行时声称至少其中一个签名与 P/Invoke 不兼容(尽管我很确定我编组了所有重要的东西)。

这是我的代码(其中 TokenType 是一个巨大的枚举,而 Failure 是一个小枚举):

public enum Failure {
    UnterminatedStringLiteral,
    UnlexableCharacter,
    UnterminatedComment
};
public enum TokenType {
    OpenBracket,
    CloseBracket,
    Dot,
    Semicolon,
    Identifier,
    String,
    LeftShift,
    RightShift,
    OpenCurlyBracket,
    CloseCurlyBracket,
    Return,
    Assignment,
    VarCreate,
    Comma,
    Integer,
    Using,
    Prolog,
    Module,
    If,
    Else,
    EqCmp,
    Exclaim,
    While,
    NotEqCmp,
    This,
    Type,
    Operator,
    Function,
    OpenSquareBracket,
    CloseSquareBracket,
    Colon,
    Dereference,
    PointerAccess,
    Negate,
    Plus,
    Increment,
    Decrement,
    Minus,

    LT,
    LTE,
    GT,
    GTE,
    Or,
    And,
    Xor
}

[StructLayout(LayoutKind.Sequential)]
private struct MaybeByte {
    public byte asciichar;
    [MarshalAs(UnmanagedType.I1)]
    public bool present;
}

[StructLayout(LayoutKind.Sequential)]
public struct Position {
    public UInt32 column;
    public UInt32 line;
    public UInt32 offset;
}

[StructLayout(LayoutKind.Sequential)]
public struct Range {
    public Position begin;
    public Position end;
}

[StructLayout(LayoutKind.Sequential)]
public struct Token {
    public Range location;
    public TokenType type;
    [MarshalAs(UnmanagedType.LPStr)]
    public string value;
}

private delegate MaybeByte LexerCallback(System.IntPtr arg);
public delegate void CommentCallback(Range arg);
private delegate Token ErrorCallback(Position p, Failure f);

[DllImport("CAPI.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern System.IntPtr CreateLexer(
    [MarshalAs(UnmanagedType.FunctionPtr)]LexerCallback callback,
    System.IntPtr context,
    [MarshalAs(UnmanagedType.FunctionPtr)]CommentCallback comment,
    [MarshalAs(UnmanagedType.FunctionPtr)]ErrorCallback error
);

一旦我用一些委托调用 CreateLexer,运行时就会出错。我看到的其他答案都是关于结构中的固定大小数组,我没有。

有什么建议么?

4

1 回答 1

1

好的。结果发现编组器拒绝了

[StructLayout(LayoutKind.Sequential)]
public struct Token {
    public Range location;
    public TokenType type;
    [MarshalAs(UnmanagedType.LPStr)]
    public string value;
}

显然,您必须在结构上明确指定 CharSet,即使 LPStr 文档清楚地说明了预期的编码。在结构上设置 CharSet = CharSet.Ansi 后,编组器拒绝了其他内容。

于 2013-06-13T13:44:05.673 回答