我正在使用 Lua 调用 dll 文件。我的 c/c++ 代码是这样的:
#include <iostream>
extern "C"{
#include "lua.hpp"
#include "lualib.h"
#include "lauxlib.h"
}
using namespace std;
typedef struct StructA{
int a;
} structA;
typedef struct StructB{
char *b;
} structB;
static int createA(lua_State *L)
{
int n = lua_gettop(L);
size_t iBytes = sizeof(struct StructA);
struct StructA *sa;
sa = (StructA *)lua_newuserdata(L, iBytes);
sa->a = 1;
return 1;
}
static int createB(lua_State *L)
{
int n = lua_gettop(L);
size_t iBytes = sizeof(struct StructB);
struct StructB *sb;
sb = (StructB *)lua_newuserdata(L, iBytes);
sb->b = "2";
return 1;
}
static int print(lua_State *L)
{
if("some condition")//struct type is StructA
{
cout<< "struct type is StructA" <<endl;
struct StructA *sa = (struct StructA *)lua_touserdata(L, 1);
cout<< "a:"<<(sa->a) <<endl;
}else if("some condition")//struct type is structB
{
cout<< "struct type is StructB" <<endl;
struct StructB *sb = (struct StructB *)lua_touserdata(L, 1);
cout<< "b:"<<(sb->b) <<endl;
}
return 0;
}
static luaL_Reg mylibs[] = {
{ "A", createA },
{ "B", createB },
{ "print", print },
{ NULL, NULL }
};
extern "C" __declspec(dllexport)
int luaopen_occi_test(lua_State* L)
{
luaL_register(L, "occi_test", mylibs);
return 1;
}
我使用 VS2010 编译了这个 C/C++ 代码。我的 Lua 代码是这样的:
local a = occi_test.A()
local b = occi_test.B()
occi_test.print(a)
occi_test.print(b)
如您所见,在Lua中,参数a和b都是userdata类型,但在C/C++中,a的类型是StructA,b的类型是StrcutB,所以当Lua调用C/C++函数并传递a或b时,C如何/C++ 代码找出参数的类型?请通过将“某些条件”替换为可以完成工作的东西来完成此代码。