Lua Launchers

lua-users home
wiki

Minimal Lua Launcher for Windows

A very basic implementation of an executable that loads and runs a Lua file.

Usage is simple:

1. Compile as luancher.exe
2. Copy luancher.exe to myapp.exe
3. Create myapp.lua in same folder
4. Implement startup code in myapp.lua
5. Run 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;
}

RecentChanges · preferences
edit · history
Last edited April 1, 2013 7:08 am GMT (diff)