//-------以下是Lua脚本-------- //--test.lua //LuaC_MessageBox( "Last is ShowMessage! This is real MessageBox!"); //------------以下是test.cpp文件---------------- //================================================================================================================ // Lua Test Object // C++ Source lua_test.cpp //================================================================================================================ //================================================================================================================ // Include Files //================================================================================================================ extern "C" { #include "D:\\My Documents\\Visual Studio 2005\\Projects\\lua\\lua\\lua.h" #include "D:\\My Documents\\Visual Studio 2005\\Projects\\lua\\lua\\lualib.h" #include "D:\\My Documents\\Visual Studio 2005\\Projects\\lua\\lua\\lauxlib.h" } #include <windows.h> #include <stdio.h> #include <string> using namespace std; //================================================================================================================ // Libraries //================================================================================================================ #pragma comment( lib ,"D:\\My Documents\\Visual Studio 2005\\Projects\\lua\\release\\lua.lib") //================================================================================================================ // Global Variables //================================================================================================================ lua_State *L; //================================================================================================================ // Lua Functions //================================================================================================================ double f( double x, double y ) { double ret; lua_getglobal( L, "f"); lua_pushnumber( L,x); lua_pushnumber( L,y); lua_call( L, 2, 1); //lua_pcall( L, 2, 1, 0); ret = lua_tonumber( L, -1); //lua_pop( L, 1); return ret; } //================================================================================================================ // C/C++ Functions //================================================================================================================ int LuaC_MessageBox( lua_State *L) { char Message[256] = ""; int i; // 获取参数个数 int n = lua_gettop(L); // 保存全部参数 for ( i = 1, j = 0; i <= n ; i++) { if ( lua_isstring( L, i)) strcpy( Message, lua_tostring( L, i)); } // 执行逻辑 MessageBox( NULL, Message, "Lua Test", MB_OK ); // 返回值压栈 // 返回压栈参数的个数 return 0; } //================================================================================================================ // Main Functions //================================================================================================================ int main( void) { int error; L = lua_open(); luaopen_base(L); luaL_openlibs(L); // 注册C/C++函数 lua_register( L, "LuaC_MessageBox", LuaC_MessageBox); // load the script // 加入了错误处理 if ( (error = luaL_dofile(L, "test.lua")) != 0) { MessageBox( NULL, "出错啦:执行脚本出错!", "Lua Test", MB_OK ); return 0; } getchar(); lua_close( L); return 1; }