getInfo method

Implementation

@visibleForTesting
WindowsDeviceInfo getInfo() {
  final systemInfo = calloc<SYSTEM_INFO>();
  final osVersionInfo = calloc<OSVERSIONINFOEX>()
    ..ref.dwOSVersionInfoSize = sizeOf<OSVERSIONINFOEX>();

  try {
    final currentVersionKey = Registry.openPath(RegistryHive.localMachine,
        path: r'SOFTWARE\Microsoft\Windows NT\CurrentVersion');
    final buildLab = currentVersionKey.getValueAsString('BuildLab') ?? '';
    final buildLabEx = currentVersionKey.getValueAsString('BuildLabEx') ?? '';
    final digitalProductIdValue =
        currentVersionKey.getValue('DigitalProductId');
    final digitalProductId = digitalProductIdValue != null &&
            digitalProductIdValue.data is Uint8List
        ? digitalProductIdValue.data as Uint8List
        : [] as Uint8List;
    final displayVersion =
        currentVersionKey.getValueAsString('DisplayVersion') ?? '';
    final editionId = currentVersionKey.getValueAsString('EditionID') ?? '';
    final installDate = DateTime.fromMillisecondsSinceEpoch(
        1000 * (currentVersionKey.getValueAsInt('InstallDate') ?? 0));
    final productId = currentVersionKey.getValueAsString('ProductID') ?? '';
    var productName = currentVersionKey.getValueAsString('ProductName') ?? '';
    final registeredOwner =
        currentVersionKey.getValueAsString('RegisteredOwner') ?? '';
    final releaseId = currentVersionKey.getValueAsString('ReleaseId') ?? '';

    final sqmClientKey = Registry.openPath(RegistryHive.localMachine,
        path: r'SOFTWARE\Microsoft\SQMClient');
    final machineId = sqmClientKey.getValueAsString('MachineId') ?? '';

    GetSystemInfo(systemInfo);

    // Use `RtlGetVersion` from `ntdll.dll` to get the Windows version.
    RtlGetVersion(osVersionInfo);

    // Handle [productName] for Windows 11 separately (as per Raymond Chen's comment).
    // https://stackoverflow.com/questions/69460588/how-can-i-find-the-windows-product-name-in-windows-11
    if (osVersionInfo.ref.dwBuildNumber >= 22000) {
      productName = productName.replaceAll('10', '11');
    }
    final data = WindowsDeviceInfo(
      numberOfCores: systemInfo.ref.dwNumberOfProcessors,
      computerName: getComputerName(),
      systemMemoryInMegabytes: getSystemMemoryInMegabytes(),
      userName: getUserName(),
      majorVersion: osVersionInfo.ref.dwMajorVersion,
      minorVersion: osVersionInfo.ref.dwMinorVersion,
      buildNumber: osVersionInfo.ref.dwBuildNumber,
      platformId: osVersionInfo.ref.dwPlatformId,
      csdVersion: osVersionInfo.ref.szCSDVersion,
      servicePackMajor: osVersionInfo.ref.wServicePackMajor,
      servicePackMinor: osVersionInfo.ref.wServicePackMinor,
      suitMask: osVersionInfo.ref.wSuiteMask,
      productType: osVersionInfo.ref.wProductType,
      reserved: osVersionInfo.ref.wReserved,
      buildLab: buildLab,
      buildLabEx: buildLabEx,
      digitalProductId: digitalProductId,
      displayVersion: displayVersion,
      editionId: editionId,
      installDate: installDate,
      productId: productId,
      productName: productName,
      registeredOwner: registeredOwner,
      releaseId: releaseId,
      deviceId: machineId,
    );
    return data;
  } finally {
    free(systemInfo);
    free(osVersionInfo);
  }
}