Lua 启动器 |
|
一个非常基本的可执行文件实现,用于加载和运行 Lua 文件。
使用方法很简单
#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;
}