Lua 啟動程式

lua-users home
wiki

在 Windows 中使用的 Lua 簡易啟動器

一個很基本的執行檔實現,用來載入並執行 Lua 文件。

使用很簡單

1. 編譯為 luancher.exe
2. 將 luancher.exe 複製到 myapp.exe
3. 在同一個資料夾中建立 myapp.lua
4. 在 myapp.lua 中實作啟動碼
5. 執行 myapp.exe

#include <windows.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#pragma comment(lib, "lua51.lib")

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	TCHAR fn[MAX_PATH];
	int res, len = GetModuleFileName(hInstance, fn, MAX_PATH);
	lua_State* l = luaL_newstate();
	luaL_openlibs(l);
	memcpy(&fn[len-3], "lua", 3); // .exe -> .lua
	if(GetFileAttributes(fn) == INVALID_FILE_ATTRIBUTES) // check file exists
		return 1;
	if(AttachConsole(ATTACH_PARENT_PROCESS)) { // attach std streams to console
		freopen("CONIN$", "r", stdin);
		freopen("CONOUT$", "w", stdout);
		freopen("CONOUT$", "w", stderr);
		printf("\n");
	}
	res = luaL_dofile(l, fn); // execute the lua file
	lua_close(l);
	return res;
}

最新變更 · 偏好設定
編輯 · 歷史紀錄
最後編輯時間:西元 2013 年 4 月 1 日上午 7:08 GMT (差異)