例子为网上找到的,最简单的,但一直用不了,现为修改后的代码:
#include <windows.h>
/*---------这里必须要用这种格式-----------*/
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
//dll中定义抛出函数,原型要是Lua_CFunction
/*----------定义函数--------------*/
static int MyLuaDLL_HelloWorld(lua_State* L)
{
// MessageBox(NULL,"Hello","World",MB_OK);
return 0;
}
static int MyLuaDLL_average(lua_State *L)
{
/* get number of arguments */
int n = lua_gettop(L);
double sum = 0;
int i;
/* loop through each argument */
for (i = 1; i <= n; i++)
{
/* total the arguments */
sum += lua_tonumber(L, i);
}
/* push the average */
lua_pushnumber(L, sum / n);
/* push the sum */
lua_pushnumber(L, sum);
/* return the number of results */
return 2;
}
/*-----------注册函数---------------*/
static const luaL_reg MyLuaDLLFunctions [] =
{
{"HelloWorld",MyLuaDLL_HelloWorld},
{"average",MyLuaDLL_average},
{NULL, NULL}
};
int __cdecl __declspec(dllexport) luaopen_MyLuaDLL(lua_State* L)
{
/*----------当初看那网上例子用的是luaL_openlib函数,但现在已经不用它了-------------*/
luaL_register(L, "MyLuaDLL", MyLuaDLLFunctions);
return 1;
}