我试图弄清楚如何将复杂对象从 C++ dll 返回到调用 C# 应用程序。我有一个简单的方法,它返回一个工作正常的 int。谁能告诉我我做错了什么?
C# 应用程序:
class Program
{
static void Main(string[] args)
{
// Error on this line: "PInvoke: Cannot return variants"
var token = LexerInterop.next_token();
}
}
C# LexerInterop 代码:
public class LexerInterop
{
[DllImport("Lexer.dll")]
public static extern object next_token();
[DllImport("Lexer.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int get_int(int i);
}
C++ 词法分析器.cpp
extern "C" __declspec(dllexport) Kaleidoscope::Token get_token()
{
int lastChar = ' ';
Kaleidoscope::Token token = Kaleidoscope::Token();
while(isspace(lastChar))
{
lastChar = getchar();
}
... Remainder of method body removed ...
return token;
}
extern "C" __declspec(dllexport) int get_int(int i)
{
return i * 2;
}
C++ 词法分析器.h
#include "Token.h"
namespace Kaleidoscope
{
class Lexer
{
public:
int get_int();
Token get_token();
};
}
C++ 令牌.h
#include <string>
namespace Kaleidoscope
{
enum TokenType
{
tok_eof = -1,
tok_def = -2,
tok_extern = -3,
tok_identifier = -4,
tok_number = -5
};
class Token
{
public:
TokenType Type;
std::string IdentifierString;
double NumberValue;
};
}
我确定这与 Token 返回类型有关,但我找不到任何像样的信息丢失了什么。
任何帮助或方向表示赞赏!