루아 확장을 어떻게 작성합니까

2423 단어 lua
DLL  :
#ifdef __cplusplus

extern "C" {

#endif



#include <lua/lua.h>

#include <lua/lualib.h>

#include <lua/lauxlib.h>



#ifdef __cplusplus

}

#endif



#pragma comment( lib, "lua51.lib" )



#define TOSTRING(x) #x

#define EXPORT_SYMBOL_TO_LUA(str) { TOSTRING(str), str }



static int MsgBox(lua_State* L)

{

    const char* pStr = lua_tostring(L, -1);

    MessageBoxA( NULL, pStr, "message", MB_OK );

    return 0;

}



static const luaL_reg MyDLLFunctions [] =

{

    EXPORT_SYMBOL_TO_LUA(MsgBox),

    {NULL, NULL}

};

//       ,          

extern "C" int __declspec(dllexport) luaopen_DllForLua(lua_State* L)

{

    luaL_openlib(L, "MyExt", MyDLLFunctions, 0);

    return 1;

}

Lua    :
n = 1;

if n == 1 then					--     

	local lib = package.loadlib("./Debug/DllForLua.dll", "luaopen_DllForLua");

	if lib then

		lib()                       --       

	else

		print("Load Dll Error!");

	end
elseif n == 2 then				--     

	package.cpath = string.format("%s;./Debug/?.dll", package.cpath);

	require("DllForLua")

end



MyExt.MsgBox("Everything is OK!");

좋은 웹페이지 즐겨찾기