Notes
- You can define array fields using the [size] notation after the corresponding field type character.
- For example, the signature
"i[5]"
defines a field that contains an array of 5 integers.
Struct.constructor(signature, fieldname1, fieldname2...) constructor
The Struct constructor creates a new C Struct definition, that can be used later to create new C struct instances (see Calling C Struct definition).
The Struct definition is used to define each field and to calculate the Struct 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 the key and the Struct definition object as value.
Example
local c = require "c"
-- Creates a COORD Struct definition
local COORD = c.Struct("ss", "x", "y")
-- Creates a SMALL_RECT Struct definition
local SMALL_RECT = c.Struct("ssss", "Left", "Top", "Right", "Bottom")
-- Creates a CONSOLE_SCREEN_BUFFER_INFO Struct definition that uses COORD and SMALL_RECT sub structs
local CONSOLE_SCREEN_BUFFER_INFO = c.Struct("..I..", {dwSize = COORD}, {dwCursorPosition = COORD}, "dwAttributes", {srWindow = SMALL_RECT}, {dwMaximumWindowSize = COORD})
-- New instance of CONSOLE_SCREEN_BUFFER_INFO struct initialized with a table
local csbi = CONSOLE_SCREEN_BUFFER_INFO {
dwSize = COORD { x = 80, y = 120 },
dwCursorPosition = COORD { x = 1, y = 1 },
dwAttributes = 1024,
srWindow = SMALL_RECT { Left = 0, Top = 0, Right = 480, Bottom = 320 },
dwMaximumWindowSize = COORD { x = 1920, y = 1080 }
}
-- prints 480
print(csbi.srWindow.Right)