#include <stdio.h>
#include <string> #include <direct.h>
#include <windows.h>
#include <io.h> extern "C" {
#include "D:\myPath\lua\5.3\include\lua.h"
#include "D:\myPath\lua\5.3\include\lauxlib.h"
#include "D:\myPath\lua\5.3\include\lualib.h"
}
#pragma comment(lib,"D:\\myPath\\lua\\5.3\\lib\\Lua53.lib")// 该模块和lua解释器都要使用动态链接
// 因为这是lua解释器也要用到的库,使用静态链接会报错 multiple Lua VMs detected #define LUA_VERSION_NUM 503 // 使用static 防止名字污染
static int pusherror(lua_State *L, const char *info) // lua函数返回错误信息就是这么来的
{
lua_pushnil(L); // lua函数不是一出错就先返回 nil + info 的模式么?
if (info == NULL)
lua_pushstring(L, strerror(errno));
else
lua_pushfstring(L, "%s: %s", info, strerror(errno));
lua_pushinteger(L, errno);
return 3;
}
// 获取当前目录 因为会用到系统有关的目录api,所以cl编译时 运行时库选择 /MD
static int get_dir (lua_State *L)
{
#ifdef NO_GETCWD
lua_pushnil(L);
lua_pushstring(L, "Function 'getcwd' not provided by system");
return 2;
#else
char *path = NULL;
/* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
size_t size = MAX_PATH; /* initial buffer size */
int result;
while (1)
{
path = (char *)realloc(path, size);
if (!path) /* failed to allocate */
return pusherror(L, "get_dir realloc() failed");
if (_getcwd(path, size) != NULL)
{
/* success, push the path to the Lua stack */
lua_pushstring(L, path);
result = 1;
break;
}
if (errno != ERANGE) { /* unexpected error */ //就是说expected的error 是ERANGE,
result = pusherror(L, "get_dir getcwd() failed");
break;
}
/* ERANGE = insufficient buffer capacity, double size and retry */
size *= 2;
}
free(path);
return result;
#endif
} static int l_split(lua_State*L){
const char* s = luaL_checkstring(L,-2);
const char* sep = luaL_checkstring(L,-1);
lua_newtable(L);
const char* e = s;
int i = 1;
while((e= strchr(s,*sep))!=NULL){
if(e-s==0){s=e+1;continue;} // 避免连续的*sep导致压入空串
lua_pushlstring(L, s, e-s);
lua_rawseti(L, -2, i++);
s = e+1;
}
lua_pushstring(L, s);
lua_rawseti(L, -2, i); return 1;
}
static int gettable(lua_State *L)
{
//lua_setfield lua_seti lua_settable luaL_ref
//lua_getfield lua_geti lua_gettable luaL_unref
// luaL_unref是删除键值对 luaL_ref是自动生成唯一键
lua_newtable(L);
char value[32] = {0};
char key[10] = {0};
for(int i = 1; i <= 5; i++)
{
sprintf(value, "value: %d", i);
lua_pushstring(L, value);
lua_seti(L, -2, i);
} for(int i = 6; i <= 10; ++i)
{
sprintf(key, "key %d", i);
sprintf(value, "value %d", i);
lua_pushstring(L, value);
lua_setfield(L, -2, key);
} for(int i = 11; i <= 15; ++i)
{
sprintf(key, "key %d", i);
sprintf(value, "value %d", i);
lua_pushstring(L, key);
lua_pushstring(L, value);
lua_settable(L, -3);
} for(int i = 16; i <= 20; ++i)
{
sprintf(value, "value %d", i);
lua_pushstring(L, value);
luaL_ref(L, -2); // 这个 不用管key, luaL_ref是自动生成唯一的key
} return 1;
} // assumes the table is on the top of stack
static int printtable(lua_State *L)
{
if(!lua_istable(L, -1))return 0;
lua_len(L, -1);
int nlen = lua_tointeger(L, -1);
lua_pop(L, 1);// 注意 这里不是索引而是数量
printf("table length:%d\n",nlen);
for(int i = 1; i <= nlen; ++i)
{
lua_geti(L, -1, i);
int t = lua_type(L, -1);
switch(t)
{
case LUA_TSTRING:
printf("%s\n", lua_tostring(L, -1));
break;
case LUA_TNUMBER:
{
if(lua_isinteger(L, -1))printf("%d\n", lua_tointeger(L, -1) );
else printf("%f\n", lua_tonumber(L, -1));
}
break;
default:printf("other type\n");
}
lua_pop(L, 1);
}
return 0;
}
static const struct luaL_Reg mylib[] =
{
{"currentdir", get_dir},
{"l_split", split},
{"gettable", gettable},
{"printtable",printtable},
{NULL, NULL},
}; extern "C" __declspec(dllexport) int luaopen_mylib(lua_State *L) // 必须是luaopen_+dll名的形式
//requre(name)=>会找name.dll 找到name.dll 中的 luaopen_name导出函数
{
//luaL_register(L,"mylib",mylib); // 已弃用 会污染全局namespace //前面还可以加一些元表信息
//设置 __gc __index等写法
luaL_newlib(L, mylib);
// lua_setglobal(L, "mylib"); // 有了这一句就可以不用mylib = require("mylib")了 lua_pushliteral(L, "Copyright (C) 2003-2016 Kepler Project");
lua_setfield(L, -2, "_COPYRIGHT");
lua_pushliteral(L, "1.0 ");
lua_setfield(L, -2, "_VERSION");
return 1;
}

我的第一份供lua调用的c模块的更多相关文章

  1. cocos进阶教程(1)Lua调用自定义C++类和函数的最佳实践

    第一层:纯C环境下,把C函数注册进Lua环境 a.lua 文件 )) a.c 文件 #include <lua.h> #include <lualib.h> #include ...

  2. Cocos2d-x下Lua调用自定义C++类和函数的最佳实践[转]

    Cocos2d-x下Lua调用C++这事之所以看起来这么复杂.网上所有的文档都没讲清楚,是因为存在5个层面的知识点: 1.在纯C环境下,把C函数注册进Lua环境,理解Lua和C之间可以互相调用的本质 ...

  3. Lua与C++交互初探之Lua调用C++

    Lua与C++交互初探之Lua调用C++ 上一篇我们已经成功将Lua的运行环境搭建了起来,也成功在C++里调用了Lua函数.今天我来讲解一下如何在Lua里调用C++函数. Lua作为一个轻量级脚本语言 ...

  4. 【转】Cocos2d-x下Lua调用自定义C++类和函数的最佳实践

    转自:http://segmentfault.com/blog/hongliang/1190000000631630 关于cocos2d-x下Lua调用C++的文档看了不少,但没有一篇真正把这事给讲明 ...

  5. lua调用java java调用lua[转载]

    转载:http://dualface.github.io/blog/2013/01/01/call-java-from-lua/LuaJavaBridge - Lua 与 Java 互操作的简单解决方 ...

  6. 用IKVMC将jar转成dll供c#调用

    用IKVMC将jar转成dll供c#调用 ikvmc c# dll jar 用IKVMC将jar转成dll供c#调用 前言 ikvmc介绍 ikvmc下载安装 下载并解压 设置环境变量 jar-> ...

  7. (原+译)LUA调用C函数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5804924.html 原始网址: http://www.troubleshooters.com/cod ...

  8. lua调用C语言

    在上一篇文章(C调用lua函数)中,讲述了如何用c语言调用lua函数,通常,A语言能调用B语言,反过来也是成立的.正如Java 与c语言之间使用JNI来互调,Lua与C也可以互调.   当lua调用c ...

  9. lua调用c++函数返回值作用

    2015/05/28 lua调用c++接口,返回给lua函数的是压入栈的内容,可以有多个返回值.但是c++接口本身也是有返回值的,这个返回值也非常的重要,会决定最后返回到lua函数的值的个数. (1) ...

随机推荐

  1. HTTP_REFERER

    .htaccess可以禁止某个来源(referer)的访问,当某个网站对你的网站图片或CSS等文件直接引用的时候,禁止其访问是避免更大损失的关键. RewriteEngine onRewriteCon ...

  2. 关于EM,REM,PX的几点注意

    px是绝对单位,不支持IE的缩放,em是相对单位. em指字体高,任意浏览器的默认字体高都是16px.所以未经调整的浏览器都符合: 1em=16px.那么12px=0.75em, 10px=0.625 ...

  3. daydayup3 codeforces144C

    上古的c还是很简单的,一直逗比忘记加EOF了,直到看了数据才发现 题意:给你两个字符串a,b,求问字符串a里有多少个子串排列后可以生成字符串b,‘?’可以替换为任意小写字母 思路:统计第一个子字符串小 ...

  4. 160809208沈昊辰c语言程序设计实验选择结构设计

    <C语言程序设计>实验报告 学 号 160809208 姓 名 沈昊辰 专业.班 计科16-2班 学    期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C区二层机房 ...

  5. 开启Win7系统管理员Administrator账户

    Win7系统凭借酷炫的界面以及简单.易用.快速.安全等特点,迅速成为全球最受用户喜爱的操作系统,如今Win7已经成为身边很多朋友生活学习工作的好伙伴.在我们使用Win7的时候,有一些软件的正常运行需要 ...

  6. C++中的static关键字的总结

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...

  7. Jmeter性能测试入门(转)

    出处:http://www.cnblogs.com/by-dream/p/5611555.html Jmeter性能测试步骤 1. 添加线程组之后,先设置这两项: 2. 添加一个http请求 被测的u ...

  8. C++ TR1 Function Bind

    在C++ 11出现以前,C++的事件一般是通过回调形试来实现,如 void (*func)(int,int,int),其实际上是一种函数指针,在C中调用时是直接写函数名在参数列表中,而在C++中,大部 ...

  9. 用PowerMock mock final类constructors

    也相对简单,直接贴代码 被测方法 public class EmployeeServiceWithParam { public void createEmployee(final Employee e ...

  10. Levenberg-Marquardt算法基础知识

    Levenberg-Marquardt算法基础知识 (2013-01-07 16:56:17) 转载▼   什么是最优化?Levenberg-Marquardt算法是最优化算法中的一种.最优化是寻找使 ...