C中调用LUA回调(LUA注册表)
实现原理:
通过将LUA中得回调函数存入LUA注册表中来保存LUA函数,然后在需要回调时从LUA注册表中取出LUA函数进行调用
下面是一些预备知识:(学习两个重要的函数)
原汁原味的英文解释的最透彻,翻译的话就会加入自己的理解
LUA_GLOBALSINDEX
int luaL_ref (lua_State *L, int t);
Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object).
A reference is a unique integer key. As long as you do not manually add integer keys into table t, luaL_ref ensures the uniqueness of the key it returns. You can retrieve an object referred by reference r by calling lua_rawgeti(L, t, r). Function luaL_unref frees a reference and its associated object.
If the object at the top of the stack is nil, luaL_ref returns the constant LUA_REFNIL. The constant LUA_NOREF is guaranteed to be different from any reference returned by luaL_ref.
void luaL_unref (lua_State *L, int t, int ref);
Releases reference ref from the table at index t (see luaL_ref). The entry is removed from the table, so that the referred object can be collected. The reference ref is also freed to be used again.
If ref is LUA_NOREF or LUA_REFNIL, luaL_unref does nothing.
这样使用
int handler = luaL_ref( L , LUA_REGISTRYINDEX );
int Test_callback(lua_State* luaState)
{
int topindex = lua_gettop(luaState);
assert(topindex == );
/** check stack top is lua function */
if (!lua_isfunction(luaState, topindex))
generate_error(luaState, "param #1 is not function\n"); /** register lua function to register table and we can use with func id
luaL_ref will pops the stack top */
int luafuncid = luaL_ref(luaState, LUA_REGISTRYINDEX); { /** Wait a moment */
/** get function by ref id */
lua_rawgeti(luaState, LUA_REGISTRYINDEX, luafuncid);
if (!lua_isfunction(luaState, -))
generate_error(luaState, "callback is not a lua function\n"); /** push params */ /** call lua function */
if (lua_pcall(luaState, , , ) != ) {
/** error message on the stack top, lua_error will jump */
lua_error(luaState);
}
luaL_unref(luaState, LUA_REGISTRYINDEX, luafuncid);
}
return ; } extern int luaopen_Test_luabinding(lua_State* luaState)
{
static const struct luaL_Reg funcs[] = {
{"callback", Test_callback},
{NULL, NULL}
};
luaL_register(luaState, "test", funcs);
return ;
}
C中调用LUA回调(LUA注册表)的更多相关文章
- 最原始的COM组件调用过程(不使用注册表信息)
最原始的COM组件调用过程(不使用注册表信息) 最近因为项目的关系开始研究COM组件了,以前都认为COM过时了,所以也没怎么接触. 现在好好补补课了. 一般调用COM都是通过注册表找到它的位置, 然后 ...
- Win64 驱动内核编程-15.回调监控注册表
回调监控注册表 在 WIN32 平台上,监控注册表的手段通常是 SSDT HOOK.不过用 SSDT HOOK 的方式监控注册表实在是太麻烦了,要 HOOK 一大堆函数,还要处理一些 NT6 系统有而 ...
- 注册表的作用、bat文件中REG ADD命令添加注册表项以及bat
注册表的用途与设置 注册表是windows的核心,里面储存着大量的系统信息,说白了就是一个庞大的数据库.如果你不懂什么是数据库,那没关系,不影响你了解注册表,不过最好对数据库有所了解.注册表里面所有的 ...
- ASP.NET中如何读取和写入注册表
直接给源码: 读取注册表内容: RegistryKey regkey=Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Window ...
- 将gbk字符串转换成utf-8,存储到注册表中后,再次从注册表读取转换成gbk,有问题!!!
char *a = "新2新"; printf("gbk:'%s'\n", a); int ii; ; ii < strlen(a); ii++) { p ...
- 入侵检测中需要监控的注册表路径研究(Windows Registry Security Check)
1. Windows注册表简介 注册表(Registry,繁体中文版Windows称之为登录档)是Microsoft Windows中的一个重要的数据库,用于存储系统和应用程序的设置信息.早在Wind ...
- asp.net中通过注册表来检测是否安装Office(迅雷/QQ是否已安装)
原文 asp.net中通过注册表来检测是否安装Office(迅雷/QQ是否已安装) 检测Office是否安装以及获取安装 路径 及安装版本 代码如下 复制代码 #region 检测Office是否 ...
- NSIS:在注册表中记录安装路径以便重装或升级时读取
原文 NSIS:在注册表中记录安装路径以便重装或升级时读取 在NSIS中,这个功能是非常有用的,可以避免用户把程序安装到多个位置的尴尬. 第1步:在“安装目录选择页面”前面加入以下代码: 1 !def ...
- Django框架(九)—— 单表增删改查,在Python脚本中调用Django环境
目录 单表增删改查,在Python脚本中调用Django环境 一.数据库连接配置 二.orm创建表和字段 三.单表增删改查 1.增加数据 2.删除数据 3.修改数据 4.查询数据 四.在Python脚 ...
- inno setup读取注册表遇到的一个坑
一.背景 目前,公司针对PR开发的一个插件需要发布到64位系统上.该插件包括一个prm格式的文件和若干个DLL文件.其中,prm文件需要复制到PR公共插件目录下,DLL需要复制到Windows系统目录 ...
随机推荐
- 前端之JavaScript第一天学习(3)-JavaScript输出
JavaScript 通常用于操作 HTML 元素. 操作 HTML 元素 如需从 JavaScript 访问某个 HTML 元素,您可以使用 document.getElementById(id) ...
- PHP字符串处理常用方法
strlen("字符串");//取字符串的长度 strcmp($a,$b); //判断两个字符串是否相同,相同返回0,$a>$b返回1,$a<$b返回-1,区分大小写 ...
- HDU 5564 Clarke and digits 状压dp+矩阵加速
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...
- 【Python】网络编程
1.TCP编程 2.SocketServer模块 3.Twisted框架 4.UDP编程 1.TCP编程--TCP是面向连接的,其一般的设计如下: # encoding:utf-8 ''' Creat ...
- ios 百度地图
百度地图 中的注意事项 1. 百度地图中 使用了c++ 设置buidSeting compoileSource 为 Object-C++ 强制使用oc++编译器 2. 设置 BuidSeti ...
- 阻止浏览器关闭 区分刷新和关闭 自试IE可用
window.onbeforeunload = onbeforeunload_handler; function onbeforeunload_handler(){ if(event.clientX& ...
- [百度空间] [转] 四元数(Quaternions)
转:四元数(Quaternions) 好吧,我必须承认到目前为止我还没有完全理解四元数,我一度把四元数理解为轴.角表示的4维向量,也就在下午我才从和同事的争辩中理解了四元数不完全是角.轴这么简单,为此 ...
- hdu 1171
求能装入大小为sum/2的背包的最大价值 #include <cstdio> #include <cstdlib> #include <cmath> #includ ...
- 自定义TexturePacker插件导出自己的plist文件
原地址:http://www.cppblog.com/sunicdavy/archive/2014/02/06/205645.html cocos2dx引擎使用plist文件, 一种特殊的xml格式作 ...
- mysql if条件
#if表达式 SELECT reg_no, IF(reg_no='718170554','黄色宾利','红色宾利') FROM car WHERE reg_no IN ('718170554','12 ...