convertToHString function winrt

int convertToHString(
  1. String string
)

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

The caller is responsible for deleting the HSTRING when it is no longer used, through a call to WindowsDeleteString(HSTRING hstr), which decrements the reference count of that string. If the reference count reaches 0, the Windows Runtime deallocates the buffer.

Implementation

int convertToHString(String string) {
  final hString = calloc<HSTRING>();
  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.value;
    }
  } finally {
    free(stringPtr);
  }
}