Lua腳本調c動態庫函數時開始調不成功,通過Lua官方的Mail List才知道:c動態庫裏的註冊函數和Lua腳本的打開庫的方式要一致。
1.
c動態庫的代碼:
static int lua_msgbox(lua_State* L)
{
const char* message = luaL_checkstring(L, 1);
const char* caption = luaL_optstring(L, 2, "");
int result = MessageBox(NULL, message, caption, MB_YESNO);
lua_pushnumber(L, result);
return 1;
}
int __declspec(dllexport) luaopen_dllforlua(lua_State* L)
{
lua_register(L, "msgbox", lua_msgbox); //使用lua_register註冊函數
return 1;
}
使用lua_register函數,則Lua腳本應該寫成:
dllforlua = package.loadlib("dllforlua.dll", "luaopen_dllforlua")
dllforlua()
msgbox("Hey, it worked!", "Lua Message Box")
2.
static int lua_msgbox(lua_State* L)
{
const char* message = luaL_checkstring(L, 1);
const char* caption = luaL_optstring(L, 2, "");
int result = MessageBox(NULL, message, caption, MB_YESNO);
lua_pushnumber(L, result);
return 1;
}
static const luaL_Reg mylib[] =
{
{"msgbox", lua_msgbox},
{NULL, NULL}
};
int __declspec(dllexport) luaopen_dllforlua(lua_State* L)
{
luaL_register(L, "dllforlua", mylib);//使用luaL_register註冊函數
return 1;
}
使用luaL_register函數,則Lua腳本應該寫成:
require("dllforlua")
dllforlua.msgbox("Hey, it worked!", "Lua Message Box")