Lua语言如何调用自己编写的C DLL文件#include "lua.h"#include "lualib.h"#include "lauxlib.h"#includestatic int MyLuaDLL_HelloWorld(lua_State* L){ MessageBox(NULL,"Hello","World",MB_OK); return 0;}static int MyLuaDLL_average(lua_State *L){ int n = lua_gettop(L); double sum = 0; int i; for (i = 1; i <= n; i++) { sum += lua_tonumber(L, i); } lua_pushnumber(L, sum / n); lua_pushnumber(L, sum); 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(L, "MyLuaDLL", MyLuaDLLFunctions, 0); return 1;} -- lua code: --local testlib = package.loadlib("Lua_Dll.dll","luaopen_MyLuaDLL"); --获取DLL中抛出函数地址print (testlib) if(testlib)then testlib(); --调用DLL中抛出函数else -- ErrorendMyLuaDLL.HelloWorld();a,b=MyLuaDLL.average(23,33,3344);print("average:",a,"sum:",b);--------------------------------------------my.c文件:#include "stdafx.h"#include #include "my.h"static int pri(lua_State *L){ MessageBox(NULL,"Hello","this is a c program!",MB_OK); return 0;}static const struct luaL_Reg mylib[]={ {"pri",pri}, {NULL,NULL}};__declspec(dllexport) int luaopen_mylib (lua_State* L){ luaL_register(L,"mylib",mylib); return 1;}my.h文件:#include "lua.h" #include "lualib.h"#include "lauxlib.h"#pragma comment(lib,"lua5.1.lib")test.lua文件:local a=package.loadlib("my.dll","luaopen_mylib");a()mylib.pri()