toPstr method

PSTR toPstr({
  1. Allocator allocator = adaptiveMalloc,
})

Converts this Dart string to a null-terminated string of 8-bit Windows (ANSI) characters (PSTR).

The string is encoded as ANSI and terminated with a single NUL byte.

The returned pointer must be freed using free, unless allocated through an Arena.

This should be used for Win32 APIs that accept PSTR.

Example:

final pstr = 'Hello'.toPstr();
// Use the PSTR with Win32 APIs.
free(pstr); // Remember to free when done.

Implementation

PSTR toPstr({Allocator allocator = adaptiveMalloc}) {
  final pstr = allocator<BYTE>(length + 1);
  for (var i = 0; i < length; i++) {
    pstr[i] = codeUnitAt(i) & 0xFF;
  }
  pstr[length] = 0;
  return .new(pstr.cast());
}