简单概要
就是将C++结构体传给lua。
Lua代码
function structTest(a)
return string.format("做个测试 \r\n a.a = %d \n a.b = %d \n a.strTest = %s",
a.a,a.c.b,a.strTest);
end
代码
void CProject1Dlg::OnBnClickedButton2()
{
// 打开换为 luaL_newstate
lua_State *L = luaL_newstate() ; /* 打开 Lua */
luaL_openlibs(L); /* 加载 .lib 文件 */
// 加载脚本文件,需要放在程序目录
luaL_loadfile( L, "test.lua" );
lua_resume( L, 0 , 0);
typedef struct
{
int a;
CString strTest;
struct test
{
int b;
}c;
}A;
A a;
a.a = 10;
a.c.b = 322;
a.strTest = _T("yes");
lua_getglobal(L,"structTest");
lua_newtable(L);
lua_pushinteger(L,a.a);
lua_setfield(L,-2,"a");
lua_newtable(L);
lua_pushinteger(L,a.c.b);
lua_setfield(L,-2,"b");
lua_setfield(L,-2,"c");
lua_pushstring(L,CStringA(a.strTest).GetString());
lua_setfield(L,-2,"strTest");
lua_pcall(L,1,1,NULL); //
// 输出计算结果
CString c = CString(lua_tostring(L,-1)) ;
lua_pop(L,1) ; // 清除堆栈 清除计算结果
// 调用结束
lua_close(L);
MessageBox(c);
}