Notes
- You can define array fields using the [dimension1][dimension2]... notation after the corresponding field type character.
- For example, the signature
"i[5][2][2]"defines a field that contains a multidimensional array of 5x2x2 integers.
Struct:constructor(signature)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).
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 |
Return value
The constructor returns a new Struct instance.Example
--! luart-extensions
import 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)