如何在Lua脚本中通过C函数检索返回的字符串?
发布时间:2020-12-14 21:45:10 所属栏目:大数据 来源:网络整理
导读:我有一个调用C函数的Lua脚本. 目前这个函数什么也没有返回. 我想更改此函数以返回一个字符串,因此在C函数的末尾我会将字符串推入Stack. 在调用Lua脚本中,我需要取回推送的字符串值. C初始化和Lua注册 void cliInitLua( void ){ void* ud = NULL; Task task;
我有一个调用C函数的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,了解更多关于这些任务的例子. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |