我有一个调用C函数的Lua脚本. 目前这个函数什么也没有返回. 我想更改此函数以返回一个字符串,因此在C函数的末尾我会将字符串推入Stack. 在调用Lua脚本中,我需要取回推送的字符串值
          目前这个函数什么也没有返回.
我想更改此函数以返回一个字符串,因此在C函数的末尾我会将字符串推入Stack.
在调用Lua脚本中,我需要取回推送的字符串值.
C初始化和Lua注册
void cliInitLua( void )
{
   void* ud = NULL;
   Task task;
   // Create a new Lua state
   L = lua_newstate(&luaAlloc, ud);
   /* load various Lua libraries */
   luaL_openlibs(L);
   /*Register the function to be called from LUA script to execute commands*/
   lua_register(L,"CliCmd",cli_handle_lua_commands);
   //lua_close(L);
   return;
} 
 这是我的函数返回一个字符串:
static int cli_handle_lua_commands(lua_State *L){
   ...
   ...
   char* str = ....; /*Char pointer to some string*/
   lua_pushstring(L, str);
   retun 1;
} 
 这是我的Lua脚本
cliCmd("Anything here doesn't matter");
# I want to retreive the string str pushed in the c function.
 在C你有类似的东西 
  
  
 static int foo (lua_State *L) {
   int n = lua_gettop(L);
   //n is the number of arguments, use if needed
  lua_pushstring(L, str); //str is the const char* that points to your string
  return 1; //we are returning one value, the string
} 
 在Lua
lua_string = foo()
这假设您已经使用lua_register注册了您的函数
请阅读伟大的documentation,了解更多关于这些任务的例子.
