Error to get Windows 10 version with Delphi

Asked

Viewed 1,945 times

6

I am trying to get the version of Windows 10 on my system, but the most it displays is Windows 8. Neither 8.1 it displays. I couldn’t find the mistake.

// Função Sistema Operacional
function WinVersion: string;
var
  VersionInfo: TOSVersionInfo;
begin
  VersionInfo.dwOSVersionInfoSize:=SizeOf(VersionInfo);
  GetVersionEx(VersionInfo);
  Result:='';
  with VersionInfo do
  begin
    case dwPlatformId of
      1:
        case dwMinorVersion of
          0:  Result:='Microsoft Windows 95';
          10: Result:='Microsoft Windows 98';
          90: Result:='Microsoft Windows Me';
        end;
      2:
    case dwMajorVersion of
      5:
        case dwMinorVersion of
          0:
            Result := 'Microsoft Windows 2000';
          1:
            Result := 'Microsoft Windows XP';
          2:
            Result := 'Microsoft Windows Server 2003';
        end;
      6:
        case dwMinorVersion of
          0:
            Result := 'Microsoft Windows Vista';
          1:
            Result := 'Microsoft Windows 7';
          2:
            Result := 'Microsoft Windows 8';
          3:
            Result := 'Microsoft Windows 8.1';
        end;
      10:
        case dwMajorVersion of
          0:
             Result := 'Microsoft Windows 10';
            end;
        end;
    end; 
  end; 
  if (Result='') then
    Result:='Sistema operacional desconhecido.';
end;
// Fim da Função SO

1 answer

10


Its algorithm is even correct, the problem is that Microsoft when releasing a new version of Windows it changes the others to MinorVersion, making your algorithm outdated and incorrectly reporting the version!

Running your algorithm, I was shown the version of Case dwMajorVersion 6 and dwMinorVersion 3, returning that my Windows would be Windows 8.1, but.... How I’m using Windows 10 Pro in the latest updates (I’m Insider) Major is 6 and mine Minor is 3.

The most effective way to get this information and with 100% certainty is to read the Windows logs.

Here is an example of how to do:

State in uses of your project Registry

function frmTeste.ObterVersaoWindows: String;
var
  vNome,
  vVersao,
  vCurrentBuild: String;
  Reg: TRegistry;
begin
  Reg         := TRegistry.Create; //Criando um Registro na Memória
  Reg.Access  := KEY_READ; //Colocando nosso Registro em modo Leitura
  Reg.RootKey := HKEY_LOCAL_MACHINE; //Definindo a Raiz

  //Abrindo a chave desejada
  Reg.OpenKey('\SOFTWARE\Microsoft\Windows NT\CurrentVersion\', true); 

  //Obtendo os Parâmetros desejados
  vNome         := Reg.ReadString('ProductName');
  vVersao       := Reg.ReadString('CurrentVersion');
  vCurrentBuild := Reg.ReadString('CurrentBuild');

  //Montando uma String com a Versão e alguns detalhes
  Result := vNome + ' - ' + vVersao + ' - ' + vCurrentBuild;
end;

To use enough:

ComponenteX.Caption := ObterVersaoWindows;
ComponenteX.Text    := ObterVersaoWindows;
ShowMessage(ObterVersaoWindows);
etc... etc...

Until more, I await your Feedback!

  • 1

    Perfect thank you!

  • 1

    Very good, I tested on Windows 8, 10 and on the server and it worked, congratulations.

  • Thank you very much, it worked right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.