Bridging

Mapping C to Lua

The entire Windows API has been consumed via official metadata and parsed as accurately as possible in order to emit proper C bindings between Lua code and Win32 APIs. Most (if not all) functions, constants, structs, unions, enums, typedefs, and callbacks in the official Windows C APIs have thus been made available to your Lua code.

Lua to C primitive mapping

Primitives that interface directly at the border of the Lua and C API, such as function and callback parameters and return values, and struct and union fields, are mapped directly between corresponding types.

Lua nil <—> C NULL
Lua bools <—> C BOOLs/BOOLEANs
Lua integers <—> C UINT64s
Lua numbers <—> C doubles
Lua strings <—> C char*
Lua userdata <—> C pointers

Note, however, that not all values from the Windows API metadata are given types that map as cleanly to Lua's semantics as these. For example, some functions may use an int to represent a boolean value, in which case, Lua code will need to check its value explicitly, and not rely on whether it is non-zero, as in C.

Constructors

Create structs, unions, and typedefs, or arrays of any of these, by calling or indexing its type object. Indexing the type object returns enough memory to contain the given number of that type. The return value is a Lua object representing raw memory of the given size. Since all returned objects are pointers to the given memory, there is no difference between an object an an array of objects. All memory is zeroed and garbage collectable.

Type accessors

All structs and unions have metatables that allow setting and getting fields. Thus mem.foo gets or sets the "foo" field at the given memory. The metatables of each type keep track of the size and type of each field. Additionally, free-floating accessors are available as keys on each type, in order to achieve something like C-casts in Lua.

Callbacks

Many C functions and structs expect C functions as callbacks. Callback types are constructors that take a Lua function and return a void* suitable for such callbacks.

Unicode support

UTF-8 is enabled for ANSI functions. This works automatically with Lua strings. When calling wide-char functions, use the L() helper to create unicode WCHAR* strings. All ANSI functions with an A-suffix are aliased without it, so that MessageBox==MessageBoxA.

Error handling

Errors are handled by handleerror(e) if set as a global.