Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Windows version in Inno Setup

I'm using Inno Setup to change the recycle bin in the OS. I need to make some cases for if the user is running Windows 7 or Windows XP. I try using:

if not FileExists(winDir + '\System32\imageres.dll') then
  if not FileExists(winDir + '\System32\shell32.dll') then
    installError(3);

But it seems like it can't find imageres.dll or shell32.dll even though I've verified they exist. What am I doing wrong? Or can I check the Windows version another way?


1 Answers

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;

// Windows 11 has the same major.minor as Windows 10.
// So it has to be distinguished by the Build.
// The IsWindows10OrNewer condition is actually redundant.
// Once we have to test for Windows 11 using the build number, we could actually
// unify and simplify all the tests above to use the build numbers only too.
function IsWindows11OrNewer: Boolean;
var
  Version: TWindowsVersion;
begin
  GetWindowsVersionEx(Version);
  Result := IsWindows10OrNewer and (Version.Build >= 22000);
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


For version checking to work correctly on modern versions of Windows, make sure you always use the latest version of Inno Setup.

like image 87
Martin Prikryl Avatar answered Mar 22 '26 05:03

Martin Prikryl