Display simple message box in Lua

iolua

It sounds like a "let me google it for you" type of a question, but after some substantial amount of research, I couldn't find the answer.

Is there a built-in message box method in Lua? If not, what is the "standard" way of doing that?

Best Answer

A message box is a GUI element and, as with many languages, not part of the standard. Either you use an external library (list), system libraries/native functions (LuaJIT FFI) or extend your interpreter with a Lua C function.

I'd prefer LuaJIT. An example for windows:

local ffi = require("ffi")  -- Load FFI module (instance)

local user32 = ffi.load("user32")   -- Load User32 DLL handle

ffi.cdef([[
enum{
    MB_OK = 0x00000000L,
    MB_ICONINFORMATION = 0x00000040L
};

typedef void* HANDLE;
typedef HANDLE HWND;
typedef const char* LPCSTR;
typedef unsigned UINT;

int MessageBoxA(HWND, LPCSTR, LPCSTR, UINT);
]]) -- Define C -> Lua interpretation

user32.MessageBoxA(nil, "Hello world!", "My message", ffi.C.MB_OK + ffi.C.MB_ICONINFORMATION)   -- Call C function 'MessageBoxA' from User32
Related Topic