如何在Lua脚本中通过C函数调用实现字符串检索并返回?
- 内容介绍
- 相关推荐
本文共计335个文字,预计阅读时间需要2分钟。
我有一个调用C函数的Lua脚本。目前这个函数没有任何返回值。我想修改这个函数以返回一个字符串,因为在这个C函数的末尾我会将字符串推入栈。在调用Lua脚本时,我需要获取这个返回的字符串值。
我有一个调用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,了解更多关于这些任务的例子.
本文共计335个文字,预计阅读时间需要2分钟。
我有一个调用C函数的Lua脚本。目前这个函数没有任何返回值。我想修改这个函数以返回一个字符串,因为在这个C函数的末尾我会将字符串推入栈。在调用Lua脚本时,我需要获取这个返回的字符串值。
我有一个调用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,了解更多关于这些任务的例子.

