1
2 #include "stdafx.h"
3
4 #include <iostream>
5 using namespace std;
6
7 extern "C"{
8 #include "lua.h"
9 #include "lualib.h"
10 #include "lauxlib.h"
11 }
12
13 lua_State *L;
14
15 int _tmain(int argc, _TCHAR* argv[])
16 {
17 //* 創建一個指向Lua解釋器的指針。
18 L = lua_open();
19 //* 函數加載Lua庫
20 luaL_openlibs(L);
21 //* 加載Lua腳本
22 luaL_dofile(L, "../TestException/add.lua");
23
24 int x = 3, y = 8;
25 //函數名
26 lua_getglobal(L, "add");
27 //第一個參數壓棧
28 lua_pushnumber(L, x);
29 //第二個參數壓棧
30 lua_pushnumber(L, y);
31 //調用函數
32 lua_call(L, 2, 1);
33 //得到返回值
34 int sum = (int)lua_tonumber(L, -1);
35 lua_pop(L, 1);
36 cout << sum << endl;
37
38 //* 關閉釋放資源
39 lua_close(L);
40
41 waitplease;
42 return 0;
43 }
44
45