2008. október 27., hétfő

How to detect the Windows OS version


Problem/Question/Abstract:

How to detect the Windows OS version

Answer:

Solve 1:

uses
  Windows;

type
  TWinVersion = (Win32, Win9x, WinNt, WinError);

function fWinVersion: TWinVersion;
var
  GV: TOSVersionInfo;
begin
  GV.dwOSVersionInfoSize := Sizeof(GV);
  if GetVersionEx(GV) then
  begin
    case GV.dwPlatformId of
      VER_PLATFORM_WIN32s:
        Result := Win32;
      VER_PLATFORM_WIN32_WINDOWS:
        Result := Win9x;
      VER_PLATFORM_WIN32_NT:
        Result := WinNT;
    else
      Result := WinError;
    end;
  end
  else
    Result := WinError;
end;


Solve 2:

type
  PTransBuffer = ^TTransBuffer;
  TTransBuffer = array[1..4] of smallint;

const
  CInfoStr: array[1..4] of string = ('FileVersion', 'LegalCopyright', 'ProductName',
    'ProductVersion');

procedure TFrmAbout.GetVersionInfo(AVersionList: TStrings);
var
  filename: string;
  i: integer;
  infoSize: DWORD;
  ptrans: PTransBuffer;
  transStr: string;
  typeStr: string;
  value: PChar;
  verBuf: pointer;
  verSize: DWORD;
  wnd: DWORD;
begin
  AVersionList.Clear;
  filename := Application.ExeName;
  infoSize := GetFileVersioninfoSize(PChar(filename), wnd);
  if infoSize <> 0 then
  begin
    GetMem(verBuf, infoSize);
    try
      if GetFileVersionInfo(PChar(filename), wnd, infoSize, verBuf) then
      begin
        VerQueryvalue(verBuf, PChar('\VarFileInfo\Translation'), Pointer(ptrans),
          verSize);
        transStr := IntToHex(ptrans^[1], 4) + IntToHex(ptrans^[2], 4);
        for i := Low(CInfoStr) to High(CInfoStr) do
        begin
          typeStr := 'StringFileInfo\' + transStr + '\' + CInfoStr[i];
          if VerQueryvalue(verBuf, PChar(typeStr), Pointer(value), verSize) then
            {AVersionList.Add(CInfoStr[i] + ': ' + value);}
            AVersionList.Add(value);
        end
      end;
    finally
      FreeMem(verBuf);
    end;
  end;
end;


Solve 3:

Delphi 5 has a variable Win32Platform in SysUtils

var
  Win32Platform: Integer = 0;

It will have 3 values

VER_PLATFORM_32s
VER_PLATFORM_WIN32_WINDOWS
VER_PLATFORM_WIN32_WIN_NT


Solve 4:

{ ... }
case Win32MajorVersion of
  3: {NT 3.51}
    OSLabel.Caption := 'Windows NT 3.51';
  4: {WIn9x/ME, NT 4}
    case Win32MinorVersion of
      0:
        OSLabel.Caption := 'Windows 95';
      10:
        OSLabel.Caption := 'Windows 98';
      90:
        OSLabel.Caption := 'Windows ME';
    else
      if (Win32Platform and VER_PLATFORM_WIN32_NT) <> 0 then
        OSLabel.Caption := 'Windows NT 4.0'
      else
        OSLabel.Caption := 'unknown';
    end;
  5: {Win2K, XP}
    case Win32MinorVersion of
      0:
        OSLabel.Caption := 'Windows 2000';
      1:
        OSLabel.Caption := 'Windows XP or .NET server';
    else
      OSLabel.Caption := 'unknown';
    end;
else
  OSLabel.Caption := 'unknown';
end;


Solve 5:

function GetOSInfo: string;
var
  Platform: string;
  BuildNumber: integer;
begin
  case Win32MajorVersion of
    3: {NT 3.51}
      Platform := 'Windows NT 3.51';
    4: {Win9x/ ME/ NT 4}
      case Win32MinorVersion of
        0:
          Platform := 'Windows 95';
        10:
          Platform := 'Windows 98';
        90:
          Platform := 'Windows ME';
      else
        if (Win32Platform and VER_PLATFORM_WIN32_NT) <> 0 then
          Platform := 'Windows NT 4.0'
        else
          Platform := SUnknown;
      end;
    5: {Win2000/ XP}
      case Win32MinorVersion of
        0:
          Platform := 'Windows 2000';
        1:
          Platform := 'Windows XP or .NET server';
      else
        Platform := SUnknown;
      end;
  else
    Platform := SUnknown;
  end;
  case Win32Platform of
    VER_PLATFORM_WIN32_WINDOWS:
      BuildNumber := Win32BuildNumber and $0000FFFF;
    VER_PLATFORM_WIN32_NT:
      BuildNumber := Win32BuildNumber;
  else
    BuildNumber := 0;
  end;
  if (Win32Platform = VER_PLATFORM_WIN32_WINDOWS) or
    (Win32Platform = VER_PLATFORM_WIN32_NT) then
  begin
    if Win32CSDVersion = '' then
      Result := Format('%s (Build %d)', [Platform, BuildNumber])
    else
      Result := Format('%s (Build %d: %s)', [Platform, BuildNumber, Win32CSDVersion]);
  end
  else
    Result := Platform;
end;

Nincsenek megjegyzések:

Megjegyzés küldése