Union.constructor(signature, fieldname1, fieldname2...) 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).
The Union definition is used to define each field and to calculate the Union size in memory.
Parameters
signature
A string
that contains a succession of characters, each character defines the field type, in order of the provided field names :
character | argument type |
---|---|
B |
int (or bool for C++) |
c |
char |
C |
unsigned char |
s |
short / int16_t |
S |
unsigned short / uint16_t |
i |
int / int32_t |
I |
unsigned int / uint32_t |
j |
long / int32_t |
J |
unsigned long / uint32_t |
l |
long long / int64_t |
L |
unsigned long long / uint64_t |
# |
size_t |
f |
float |
d |
double |
Z |
char* / >const char* |
W |
wchar_t* / >const wchar_t* |
p |
void* |
u |
union |
. |
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.
Example
local c = require "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))