【发布时间】:2023-04-02 22:04:01
【问题描述】:
我有一个调用 C 函数的 Lua 脚本。 目前这个函数没有返回任何东西。 我想更改此函数以返回一个字符串,因此在 C 中此函数的末尾,我会将字符串推入堆栈。 在调用 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;
}
这是我的 c 函数返回一个字符串:
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.
【问题讨论】:
-
您已经拥有的代码将是一个很好的起点。
-
请参阅我的答案的“在 LUA 中”部分。
-
这是 Lua,不是 LUA。 Lua 是葡萄牙语中月亮的意思,不是首字母缩写词。
-
desculpe,我修正了我的答案。