toPcstr method

PCSTR toPcstr({
  1. Allocator allocator = adaptiveMalloc,
})

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

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

Example:

final pcstr = 'Hello'.toPcstr();
// Use the PCSTR with Win32 APIs.
free(pcstr); // Remember to free when done.

Implementation

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