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 :

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

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.

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)