convertToHString function Null safety winrt

Pointer<IntPtr> convertToHString(
  1. String string
)

Takes a Dart String and converts it to an HSTRING (a WinRT String), returning a pointer to the HSTRING.

The caller is responsible for deleting the HSTRING when it is no longer used, through a call to WindowsDeleteString().

Implementation

Pointer<IntPtr> convertToHString(String string) {
  final hString = calloc<IntPtr>();
  final stringPtr = string.toNativeUtf16();
  // Create a HSTRING representing the object
  try {
    final hr = WindowsCreateString(stringPtr, string.length, hString);
    if (FAILED(hr)) {
      throw WindowsException(hr);
    } else {
      return hString;
    }
  } finally {
    free(stringPtr);
  }
}