Determine Windows version in Inno Setup

不羁的心 提交于 2019-11-27 23:21:50

You should use the GetWindowsVersionEx function. It fills a TWindowsVersion record:

TWindowsVersion = record
  Major: Cardinal;             // Major version number
  Minor: Cardinal;             // Minor version number
  Build: Cardinal;             // Build number
  ServicePackMajor: Cardinal;  // Major version number of service pack
  ServicePackMinor: Cardinal;  // Minor version number of service pack
  NTPlatform: Boolean;         // True if an NT-based platform
  ProductType: Byte;           // Product type (see below)
  SuiteMask: Word;             // Product suites installed (see below)
end;

There are a lot of other related functions. See below 'System functions' at this page.

According to the documentation, the parameters associated with each file can be directly tied to the OS version:

[Files]
Source: "{app}\WinNT2000XP.exe"; DestDir: "{app}"; MinVersion: 0, 1
Source: "{app}\Win9598Me.exe"; DestDir: "{app}"; MinVersion: 1, 0

"0" means never install; "1" means install on any version (i.e. version 1.0 or later).

Note: The above technique isn't limited to the [Files] section; MinVersion and OnlyBelowVersion can be used in most sections.

In most Inno Setup sections (like [Files], [Tasks], [Run], etc.) you can use the MinVersion and OnlyBelowVersion common parameters.

[Files]
Source: MyDllForVistaAndNewer.dll; Dest: {app}\MyDll.dll; MinVersion: 6.0
Source: MyDllForOldWindows.dll; Dest: {app}\MyDll.dll; OnlyBelowVersion: 6.0

In Pascal Script, use the GetWindowsVersionEx function to find the Windows version number. Then compare the number against a specific Windows version number.

Here are few handy functions to check specific Windows versions:

function IsWindowsVersionOrNewer(Major, Minor: Integer): Boolean;
var
  Version: TWindowsVersion;
begin
  GetWindowsVersionEx(Version);
  Result :=
    (Version.Major > Major) or
    ((Version.Major = Major) and (Version.Minor >= Minor));
end;

function IsWindowsXPOrNewer: Boolean;
begin
  Result := IsWindowsVersionOrNewer(5, 1);
end;

function IsWindowsVistaOrNewer: Boolean;
begin
  Result := IsWindowsVersionOrNewer(6, 0);
end;

function IsWindows7OrNewer: Boolean;
begin
  Result := IsWindowsVersionOrNewer(6, 1);
end;

function IsWindows8OrNewer: Boolean;
begin
  Result := IsWindowsVersionOrNewer(6, 2);
end;

function IsWindows10OrNewer: Boolean;
begin
  Result := IsWindowsVersionOrNewer(10, 0);
end;

Example of use:

function InitializeSetup: Boolean;
begin
  if not IsWindowsVistaOrNewer then
  begin 
    MsgBox(
      'This program was not tested on Windows XP and older, proceed with caution.',
      mbCriticalError, MB_OK);
  end;  

  Result := True;
end;

To test for server-editions of Windows, see:
Checking for Windows Server 2003

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!