C++ side:
#include "" lua_State *L; void load_lua(lua_State **L,char *filename){ *L=luaL_newstate(); luaL_openlibs(*L); if(luaL_loadfile(*L,filename) || lua_pcall(*L,0,0,0)){ luaL_error(*L,"load file error! %s",lua_tostring(*L,-1)); } } int _tmain(int argc, _TCHAR* argv[]) { load_lua(&L,""); //If you pass it directly into L here, you will have an error lua_getglobal(L,"gettable"); if(lua_pcall(L,0,1,0) !=0){ luaL_error(L,"pcall wrong %s",lua_tostring(L,-1)); } luaL_checktype(L,1,LUA_TTABLE); int n=lua_objlen(L,1); printf("n = %d\n",n); lua_pushstring(L,"ee"); lua_rawseti(L,1,5); //t[n]=v,n is the third parameter, v is the top element of the stack n=lua_objlen(L,1); printf("n = %d\n",n); int i; for(i=1;i<=n;i++){ lua_rawgeti(L,1,i); printf("%s\n",lua_tostring(L,-1)); } return 0; }
lua script:
function gettable() tb={ "aa","bb","cc","dd"} return tb end
The result of running the output is:
n = 4 n = 5 aa bb cc dd ee