Last active
September 10, 2017 03:05
-
-
Save lxfly2000/4197b6f8c56c63100c8aae75a21abe39 to your computer and use it in GitHub Desktop.
Lua与C语言函数相互调用示例。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Lua与C交互示例,参考:http://blog.csdn.net/shun_fzll/article/details/39120965 | |
#include<stdio.h> | |
#include<string.h> | |
#include"lua.h" | |
#include"lualib.h" | |
#include"lauxlib.h" | |
#pragma comment(lib,"lua53.lib") | |
#define C(action,r) if(r=action)return r | |
#define LVAR_NAME "luanum" | |
const char luasource[] = | |
"function PrintSthAndHello( str )\n" | |
" print(str)\n" | |
" print(\"来自 Lua 的消息!\")\n" | |
" print(\""LVAR_NAME"的值是:\".."LVAR_NAME")\n" | |
" CallCHello()\n" | |
"end\n\n" | |
"function CallCHello()\n" | |
" r=PrintSomethingAndHello(\"在此处调用 C 的函数。\") --返回值为字符串的长度\n" | |
" print(\"返回值为\"..r)\n" | |
"end\n\n" | |
LVAR_NAME"=0"; | |
int PrintSomethingAndHello(lua_State*ls) | |
{ | |
const char *str = lua_tostring(ls, 1);//由于该函数是固定参数可直接假设只用到参数1 | |
puts(str); | |
puts("来自 C 的消息!"); | |
lua_pushinteger(ls, strlen(str));//表示这个函数的返回值是str的长度 | |
return 1;//表示这个函数会返回1个返回值 | |
} | |
lua_State *InitLua() | |
{ | |
lua_State *ls = luaL_newstate();//创建Lua环境 | |
if (!ls)return 0; | |
luaL_openlibs(ls);//加载Lua标准库 | |
return ls; | |
} | |
void ReleaseLua(lua_State *ls) | |
{ | |
lua_close(ls);//关闭Lua环境 | |
} | |
int InitLuaSource(lua_State *ls, const char *luaSource) | |
{ | |
int ret = 0; | |
if (ret = luaL_dostring(ls, luaSource))//读取并运行Lua源代码 | |
{ | |
puts(lua_tostring(ls, -1)); | |
lua_close(ls); | |
return ret; | |
} | |
return ret; | |
} | |
lua_Integer GetLuaVariable(lua_State *ls, const char *variable_name) | |
{ | |
lua_Integer r = 0; | |
lua_getglobal(ls, variable_name); | |
if (lua_isnumber(ls, -1)) | |
r = lua_tointeger(ls, -1); | |
return r; | |
} | |
int CallLuaFunction_PrintSthAndHello(lua_State *ls, const char *param) | |
{ | |
int ret = 0; | |
lua_pushinteger(ls, 30); | |
lua_setglobal(ls, LVAR_NAME); | |
lua_pop(ls, -1); | |
lua_getglobal(ls, "PrintSthAndHello");//获取函数 | |
lua_pushstring(ls, param);//将参数1放到Lua栈区 | |
if (ret = lua_pcall(ls, 1, 0, 0))//执行函数 | |
puts(lua_tostring(ls, -1)); | |
return ret; | |
} | |
#define PushFunctionToLua(luastate,funcpointer_and_name) lua_register(luastate,_CRT_STRINGIZE(funcpointer_and_name),funcpointer_and_name) | |
int main() | |
{ | |
lua_State *ls = InitLua(); | |
puts("Lua Source:==========="); | |
puts(luasource); | |
InitLuaSource(ls, luasource); | |
PushFunctionToLua(ls, PrintSomethingAndHello); | |
puts("Output:==============="); | |
printf("luanum:%lld\n", GetLuaVariable(ls, LVAR_NAME)); | |
printf("Return:===============\n%d\n", CallLuaFunction_PrintSthAndHello(ls, "此处调用 Lua.")); | |
ReleaseLua(ls); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment