Integer Types
Unlike Dart, which has a single integer type for storing values between -263-1 and 263, the Win32 API utilizes various integer types in both signed and unsigned forms.
The following table lists the common integer types encountered when working with Win32 APIs:
Common Win32 Names | dart:ffi Type | Size | Signed | Range |
---|---|---|---|---|
BYTE , UINT8 , CHAR | Uint8 | 8 bits | No | 0 to 255 |
INT8 | Int8 | 8 bits | Yes | -127 to 128 |
WORD , ATOM , UINT16 , USHORT | Uint16 | 16 bits | No | 0 to 65535 |
INT16 , SHORT | Int16 | 16 bits | Yes | -32767 to 32768 |
DWORD , UINT , UINT32 , ULONG | Uint32 | 32 bits | No | 0 to 232 |
INT , INT32 , LONG | Int32 | 32 bits | Yes | -231-1 to 231 |
DWORDLONG , ULONGLONG , UINT64 , ULONG64 | Uint64 | 64 bits | No | 0 to 264 |
LONGLONG , INT64 | Int64 | 64 bits | Yes | -263-1 to 263 |
HANDLE , LONG_PTR , UINT_PTR , ULONG_PTR | IntPtr | 32/64 bits | No | Matches pointer length |
When calling a function and passing a value, you don't need to worry about
converting from a Dart int
type to the appropriate native representation:
win32 includes all the metadata to handle this conversion for you.
However, understanding the type of data becomes crucial when a function
returns a value. In Win32, functions often use a Pointer
argument
that they fill with the relevant data, making it important to know the expected
data type.
win32 provides
typedefs
for all common Win32 integer types, so you don't need to remember the size and sign of types likeDWORD
.
Here’s an example demonstrating how to retrieve the size of system memory using win32:
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
void main() {
final lpMemorySize = calloc<ULONGLONG>();
GetPhysicallyInstalledSystemMemory(lpMemorySize);
final memorySizeMB = lpMemorySize.value ~/ 1024;
print('System has ${memorySizeMB}MB of RAM installed.');
free(lpMemorySize);
}
lpMemorySize
could also be allocated as calloc<ULONG64>()
or
calloc<Uint64>()
— they all refer to the same type.
For a more detailed demonstration of retrieving system information, see the
sysinfo.dart
example.