C和lua交互
来源:互联网 发布:php经典面试题 编辑:程序博客网 时间:2024/05/17 04:27
C调用LUA函数
query.c#include <lua.h>#include <lualib.h>#include <lauxlib.h>int main(int argc, char* argv[]){ lua_State* L = lua_open(); luaL_openlibs(L); luaL_dofile(L,"query.lua"); //加载脚本并实例化,相当于loadfile并pcall一次 lua_getglobal(L, "query_Id"); //加载lua脚本中的函数名 lua_pcall(L, 0, 0, 0); //调用lua中的该函数 return 0;}
query.luafunction query_Id() print("Id is 111")endlua_pcall函数解析:http://manual.luaer.cn/lua_pcall.html
lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);
nargs is the number of arguments that you pushed onto the stack. The number of results is adjusted tonresults.
nargs is the number of arguments that you pushed onto the stack. The number of results is adjusted tonresults.
nargs是输入参数的个数,nresults是返回值的个数。
luaL_dofile函数解析:
int luaL_dofile (lua_State *L, const char *filename);
Loads and runs the given file. It is defined as the following macro:
Loads and runs the given file. It is defined as the following macro:
(luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))It returns 0 if there are no errors or 1 in case of errors.
lua_getglobal函数解析:
LUA解释器的工作机制,LUA解释器自身维护一个运行时栈,通过这个运行时栈,LUA解释器向主机程序传递参数,所以我们可以通过这个运行时栈得到LUA脚本中变量的值。
lua_getglobal将LUA的一个全局变量放到栈顶。
LUA调用C函数
query.c#include <lua.h>#include <lualib.h>#include <lauxlib.h>struct get_ret_data{ int exist_flag; char ret_str[4096]; int ret_len;};struct get_ret_data hget_ret;int read_dba_C(lua_State *L){ int flag = lua_tonumber(L, 1); //lua传递的第一个参数 lua_pushlstring(L, get_ret.ret_str, get_ret.ret_len); //返回给lua的值 lua_pushnumber(L, mem_num); //返回给lua的值 return 2; //返回值的个数}int main(int argc, char* argv[]){ lua_State* L = lua_open(); luaL_openlibs(L); luaL_dofile(L,"query.lua"); //加载脚本并实例化,相当于loadfile并pcall一次 lua_register(L,"read_dba",read_dba_C); //注册函数,使得lua中可以调用C函数 lua_getglobal(L, "query_Id"); //加载lua脚本中的函数名 lua_pcall(L, 0, 0, 0); //调用lua中的该函数 return 0;}
query.luafunction query_Id() local flag = 1 local buf,num = read_dba(flag) print(buf,num)end
0 0
- C和lua交互
- lua和C交互
- lua和c的交互
- lua和C交互框架
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和C交互框架
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和C交互框架
- lua和c的交互
- lua和c的交互
- lua和c的交互
- C/C++和Lua的交互
- Lua和C/C++的交互
- 错误:提领指向不完全类型的指针
- 求出类似3025的数,30和25,使之相加后求平方=3025
- 《JavaScript网页特效经典300例 》
- 定制NavgationBar 返回按钮的标题 和图标
- Google App Engine中使用数据库
- C和lua交互
- Win7 64bit 安装XP32Bit打印机方法
- 网球初学者网球拍的推荐
- Android应用程序与SurfaceFlinger服务的连接过程分析
- 设计模式——依赖倒置原则
- linux 字符串转换成数组
- 浅析海水淡化膜测试回收率及污染速度
- sql server存储过程中=null和is null有什么区别?
- C++进阶必知