lua <--> c++
来源:互联网 发布:如何申请淘宝图片保护 编辑:程序博客网 时间:2024/06/07 12:05
lua与c++互调
c++头文件:
#pragma once#include <windows.h>#include <tchar.h>#include <iostream>#include <list>extern "C"{#include <lauxlib.h>#include <lualib.h>#include <lua.h>}
导入库:
#include "import.h"#pragma comment( lib, "lua5.1.lib" )#pragma comment( lib, "lua51.lib" )
c++代码:
#include "import.h"/////////////////////////////////////////////////////////////////////////////////// int load_lua( const char * lpszFile, int * lpnWidth, int * lpnHeight );int call_c_func( lua_State * L );int main( int argc, const char * argv[] ){ using namespace std; int nWidth = 0, nHeight = 0; int nRet = 0; nRet = load_lua( "d:\\lua\\test.lua", &nWidth, &nHeight ); if( 0 == nRet ) { cout << "width = " << nWidth << endl; cout << "height = " << nHeight << endl; } else { cout << "failed = " << nRet << endl; } return 0;}int load_lua( const char * lpszFile, int * lpnWidth, int * lpnHeight ){ using namespace std; int nRet = 0; lua_State * L = lua_open(); if( ( 0 == nRet ) && !L ) { nRet = 1; } if( 0 == nRet ) { lua_pushcfunction( L, call_c_func ); lua_setglobal( L, "call_c_func" ); luaopen_base( L ); luaopen_string( L ); luaopen_math( L ); } if( ( 0 == nRet ) && ( 0 != luaL_loadfile( L, lpszFile ) ) ) { cout << "luaL_loadfile failed" << endl; nRet = 1; } if( ( 0 == nRet ) && ( 0 != lua_pcall( L, 0, 0, 0 ) ) ) { cout << "lua_pcall failed" << endl; nRet = 1; } if( 0 == nRet ) { lua_getglobal( L, "f" ); lua_pushnumber( L, 1 ); lua_pushnumber( L, 2 ); nRet = lua_pcall( L, 2, 1, 0 ); if( 0 != nRet ) { cout << lua_tostring( L, -1 ) << endl; } } if( ( 0 == nRet ) && !lua_isnumber( L, -1 ) ) { cout << "ret is not a int" << endl; nRet = 1; } if( 0 == nRet ) { int ret = ( int )lua_tonumber( L, -1 ); lua_pop( L, 1 ); cout << "ret = " << ret << endl; } if( 0 == nRet ) { lua_getglobal( L, "width" ); lua_getglobal( L, "height" ); cout << lua_tostring( L, -2 ) << endl; cout << lua_tostring( L, -1 ) << endl; } if( ( 0 == nRet ) && !lua_isnumber( L, -2 ) ) { cout << "width is not a int" << endl; nRet = 1; } if( ( 0 == nRet ) && !lua_isnumber( L, -1 ) ) { cout << "height is not a int = " << lua_tostring( L, -1 ) << endl; nRet = 1; } if( 0 == nRet ) { *lpnWidth = ( int )lua_tonumber( L, -2 ); *lpnHeight = ( int )lua_tonumber( L, -1 ); } lua_close( L ); L = 0; return nRet;}int call_c_func( lua_State * L ){ using namespace std; double d = lua_tonumber( L, 1 ); double dd = d + d; cout << "input = " << d << endl; cout << "output = " << dd << endl; lua_pushnumber( L, dd ); return 1;}
lua代码:
-- this is a test-- this ssswidth= 30height = 40function f( x, y )local z = x + yprint( x, y, z )z = call_c_func( z )print( "call_c_func ret = ", z )return x + yend
输出结果:
1 2 3input = 3output = 6call_c_func ret = 6ret = 33040width = 30height = 40
- lua -->c c-->lua
- lua -->c
- Lua C
- [Lua]C/C++调用Lua
- [lua]C调用lua函数
- 【Lua】Lua与C交互
- lua与c互调--c调用lua
- lua与c互调--lua调用c
- c调lua, lua调c
- lua的C接口
- Lua 与C交互
- C/C++ 调用 Lua
- c调用lua脚本
- Lua调用C函数
- C调用lua
- Lua 与 C 交互
- LUA\SRC\LAPI.C
- lua 调用c函数
- 第四周任务(四)
- 查找最小的k个元素(数组)
- Html5 Canvas初探学习笔记(2) -绘制深入
- VC 多线程编程
- JAVA方法重载和方法重写
- lua <--> c++
- 第五周任务(一)
- 蓝牙底层HCI驱动的实现
- iphone sdk CATransition 错误
- Collections 自然数排列方法、最大值、最小值
- 揭开Socket编程的面纱
- android开发我的新浪微博客户端-大图浏览以及保存篇(7)
- c&c++中的指针
- 程序员之路──如何学习C语言并精通C语言