Getting the size of a data type

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Getting the size of a data type

26 Oct 2014, 17:48

Is there a built-in way in AHK to obtain the size of a given data type, like "int", "short", "float", "ptr", etc. ?

If not, I suppose we could write it by hand.
Are those data types always the same size independently of the version of AHK (32 or 64 bits)?
Is "int" always 4 bytes?

What about "ptr"?
In 32-bit it's 4 bytes, but in 64-bit I believe it's 8 bytes.
User avatar
LinearSpoon
Posts: 156
Joined: 29 Sep 2013, 22:55

Re: Getting the size of a data type

26 Oct 2014, 19:30

char: 1
short: 2
int: 4
int64: 8
float: 4
double: 8
ptr: A_PtrSize (built in variable)

Unsigned types are the same size as above, the only difference is that they represent a different range of values. These aren't going to change since AHK defines their sizes. Presumably if there is ever a 128 bit version of AHK, something like "int128" would be added, or whatever a suitable alternative name would be.
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Getting the size of a data type

26 Oct 2014, 19:51

How does this affect DllCall in 32 vs 64 bit ??
When a DLL function takes as input an Integer, is it up to us to detect whether we are in a 32 or 64 bit environment and give the appropriate "int" or "int64" ?

Also, what happens in the case when you have to pass a pointer to a structure of integers.
For example the RECT structure which has 4 longs.
Do we have to detect the word size of the OS and use "int" or "int64" accordingly when calling NumPut?
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Getting the size of a data type

27 Oct 2014, 00:43

When a DLL function takes as input an Integer, is it up to us to detect whether we are in a 32 or 64 bit environment and give the appropriate "int" or "int64" ?
No Int will always be Int sized.
If an Int changes it size from 32 to 64 bit on the corresponding system it is a pointer so can just use Ptr.
Also, what happens in the case when you have to pass a pointer to a structure of integers.
Sadly structures change heavily from 32 to 64 bit.
http://en.wikipedia.org/wiki/Data_structure_alignment
Also, what happens in the case when you have to pass a pointer to a structure of integers.
For example the RECT structure which has 4 longs.
This Code works for both 64 and 32 bit:

Code: Select all

VarSetCapacity(RECT,16)
Loop 4
    NumPut(A_Index,RECT,A_Index*4-4,"Int")
DllCall(Rectfunc,"Ptr",&RECT)
Do we have to detect the word size of the OS and use "int" or "int64" accordingly when calling NumPut?
The Word size is the Ptr size therefore you can use Ptr in that case.
Recommends AHK Studio
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: Getting the size of a data type

27 Oct 2014, 13:57

Thanks for your answers, nnnik and HotKeyIt.

If I undestood all correctly, I can conclude the following:
  • The only variable-sized data type in the Windows API is the pointer type. All other primitive types are always the same size irrespectively of the system's word size , as shown here: http://msdn.microsoft.com/en-us/library ... 83751.aspx
  • The size and layout of structures are only affected if they contain pointers (handles, addresses, etc.), in which case the global constant A_PtrSize holds the size of pointers for the current environment.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Getting the size of a data type

27 Oct 2014, 14:59

Getfree wrote:The only variable-sized data type in the Windows API is the pointer type.
There is also
  • TCHAR,TBYTE, Ansi 1 Byte / Unicode 2 Bytes A_IsUnicode+1
    Script Compatibility wrote:; 8-bit/ANSI strings: size_of_char=1 type_of_char="Char"
    ; 16-bit/UTF-16 strings: size_of_char=2 type_of_char="UShort"
  • HALF_PTR WORD on 32-bit and INT on 64-bit A_PtrSize/2
.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Getting the size of a data type

28 Oct 2014, 01:41

Here is a list Bentschi did
Data type <--> AHK type
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Getting the size of a data type

28 Oct 2014, 12:42

Here are some corrections including String types AStr, WStr, Str:
BOOL uint INT
HRESULT ptr INT
LANGID short USHORT
LPCSTR ptr ASTR
LPCTSTR ptr STR
LPCWSTR ptr WSTR
LPSTR ptr ASTR
LPTSTR ptr STR
LPWSTR ptr WSTR
LRESULT ptr UINT
PCSTR ptr ASTR
PCTSTR ptr STR
PCWSTR ptr WSTR
PSTR ptr ASTR
PTSTR ptr STR
PWSTR ptr WSTR
WCHAR short USHORT
User avatar
joedf
Posts: 8959
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Getting the size of a data type

28 Oct 2014, 13:06

Maybe bentschi's page should also be hosted on ahkscript.
@HotKeyIt these changes apply to Bentschi's page?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Getting the size of a data type

28 Oct 2014, 21:55

Referring to WinApi Datatypes:
CALLBACK is not a data type. It is an alias for a keyword which can be applied to a function.
#define CALLBACK __stdcall
CONST is not a data type either, but the list kind of reflects this (the AHK type column is blank).
The secondary type listed for LRESULT, "ptr*" is incorrect. It is not a pointer to a pointer sized integer. It is a pointer sized integer (LONG_PTR) with meaning dependent on which message it is used with.
PBOOL's secondary type should be int* rather than uint*, although it won't matter if the value is always TRUE or FALSE.
PDWORDLONG's secondary type should be uint64*, not ushort*.
PDWORD_PTR's and PSIZE_T's secondary type should be uptr*, not ptr*. They are unsigned.
The table row for PDWORD32 has too many columns.
PLCID's secondary type should be uint*, not ptr*.

There are probably other errors, but I've lost interest.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Getting the size of a data type

29 Oct 2014, 01:49

lexikos wrote:... but I've lost interest.
Nothing new Image (joke =)
the more documented (finished) information => its easier to work with ;)


Original Page:
Windows Data Types
Data Type Ranges
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Dewi Morgan and 377 guests