toPwstr method

PWSTR toPwstr({
  1. Allocator allocator = adaptiveMalloc,
})

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

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 PWSTR.

Example:

final pwstr = 'Hello'.toPwstr();
// Use the PWSTR with Win32 APIs.
free(pwstr); // Remember to free when done.

Implementation

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