getComputerName method

  1. @visibleForTesting
String getComputerName()

Implementation

@visibleForTesting
String getComputerName() {
  final nameLength = calloc<Uint32>();
  String name;

  GetComputerNameEx(
    COMPUTER_NAME_FORMAT.ComputerNameDnsFullyQualified,
    nullptr,
    nameLength,
  );
  final namePtr = calloc<Uint16>(nameLength.value).cast<Utf16>();
  try {
    final result = GetComputerNameEx(
        COMPUTER_NAME_FORMAT.ComputerNameDnsFullyQualified,
        namePtr,
        nameLength);

    if (result != 0) {
      name = namePtr.toDartString();
    } else {
      throw WindowsException(HRESULT_FROM_WIN32(GetLastError()));
    }
  } finally {
    calloc.free(namePtr);
    calloc.free(nameLength);
  }
  return name;
}