toPcwstr method

PCWSTR toPcwstr({
  1. Allocator allocator = adaptiveMalloc,
})

Converts this Dart string to a null-terminated UTF-16 string (PCWSTR).

The string is encoded as UTF-16 and terminated with a single NUL code unit.

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

This should be used for Win32 APIs that accept PCWSTR.

Example:

final pcwstr = 'Hello'.toPcwstr();
// Use the PCWSTR with Win32 APIs.
free(pcwstr); // Remember to free when done.

Implementation

PCWSTR toPcwstr({Allocator allocator = adaptiveMalloc}) {
  final units = codeUnits;
  final length = units.length;
  final pcwstr = allocator<WCHAR>(length + 1);
  final buffer = pcwstr.asTypedList(length + 1)..setAll(0, units);
  buffer[length] = 0;
  return .new(pcwstr.cast());
}