Union:constructor(signature, fieldname1...)constructor

The Union constructor creates a new C Union definition, that can be used later to create new C Union instances (see Calling C Union definition).

Parameters

signature

A string that contains a succession of characters, each character defines the field type, in order of the provided field names :

characterargument type
Bint (or bool for C++)
cchar
Cunsigned char
sshort / int16_t
Sunsigned short / uint16_t
iint / int32_t
Iunsigned int / uint32_t
jlong / int32_t
Junsigned long / uint32_t
llong long / int64_t
Lunsigned long long / uint64_t
#size_t
ffloat
ddouble
Zchar* / >const char*
Wwchar_t* / >const wchar_t*
pvoid*
uunion
.struct

fieldname1...

A succession of string that contains field names. They must follow the same order as the signature.
When using a field type "." for sub-struct or "u" for union, you must provide a table instead of a string, that contains the field name as a key and the Union definition object as value.

Return value

The constructor returns a new Union instance.

Example

--! luart-extensions import c -- Creates a LARGE_INTEGER C union definition from Windows API, with a struct field local LARGE_INTEGER = c.Union(".l", { Part = c.Struct("Ii", "Low", "High") }, "QuadPart") -- Creates a new LARGE_INTEGER union local int64 = LARGE_INTEGER() -- set the QuadPart field int64.QuadPart = 98320983092830928 -- prints the HighPart of the integer value print(string.format("High part of LARGE_INTEGER\t%X", int64.Part.High)) -- prints the LowPart of the integer value print(string.format("Low part of LARGE_INTEGER\t %X", int64.Part.Low)) -- prints the integer value print(string.format("Quad part for LARGE_INTEGER\t%X", int64.QuadPart))