我有一个未受管理的函数(它接受三个其他函数作为参数,使得这更加令人困惑,因为VS没有说出哪个函数是问题所在)。 .NET Runtime声称他们中至少有一个签名不兼容P/Invoke(尽管我非常确定我已经处理了所有复杂的东西)。
以下是我的代码(其中TokenType是一个巨大的枚举,而Failure是一个小枚举):
当我使用一些代理调用CreateLexer时,运行时出现错误。我看到的其他答案都与结构体中的固定大小数组有关,但我没有这样的数组。
有什么建议吗?
以下是我的代码(其中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时,运行时出现错误。我看到的其他答案都与结构体中的固定大小数组有关,但我没有这样的数组。
有什么建议吗?
Failure
定义在哪里? - Damien_The_Unbeliever