func getComputerNameEx()

in providers/windows/host_windows.go [167:196]


func getComputerNameEx(name uint32) (string, error) {
	size := uint32(64)

	for {
		buff := make([]uint16, size)
		err := stdwindows.GetComputerNameEx(
			name, &buff[0], &size)
		if err == nil {
			return syscall.UTF16ToString(buff[:size]), nil
		}

		// ERROR_MORE_DATA means buff is too small and size is set to the
		// number of bytes needed to store the FQDN. For details, see
		// https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getcomputernameexw#return-value
		if errors.Is(err, syscall.ERROR_MORE_DATA) {
			// Safeguard to avoid an infinite loop.
			if size <= uint32(len(buff)) {
				return "", fmt.Errorf(
					"windows.GetComputerNameEx returned ERROR_MORE_DATA, " +
						"but data size should fit into buffer")
			} else {
				// Grow the buffer and try again.
				buff = make([]uint16, size)
				continue
			}
		}

		return "", fmt.Errorf("could not get windows FQDN: could not get windows.ComputerNamePhysicalDnsFullyQualified: %w", err)
	}
}