Determine the operating system your app is running on¶
Sometimes, we cannot just rely on the framework, we need to make direct calls to the operating system. Too bad, there is not a single call for every operating system. Without knowing which environment is running in, it is hard to make the right decision. Back in the day, we needed to juggle around pCHAR
s, buffers and all sorts of data structures in order to determine the operating system in Delphi. Since XE2 it has become much easier as a class called TOSVersion
delivers all we need. However, a lot of Delphi developers are unaware of this wonderful helper class which is a part of System.SysUtils
:
type
TOSVersion = record
public type
TArchitecture = (arIntelX86, arIntelX64, arARM32, arARM64);
TPlatform = (pfWindows, pfMacOS, pfiOS, pfAndroid, pfWinRT, pfLinux);
public const
AllArchitectures = [arIntelX86, arIntelX64, arARM32, arARM64];
AllPlatforms = [pfWindows, pfMacOS, pfiOS, pfAndroid,
pfWinRT, pfLinux];
private
class var FArchitecture: TArchitecture;
class var FBuild: Integer;
class var FMajor: Integer;
class var FMinor: Integer;
class var FName: string;
class var FPlatform: TPlatform;
class var FServicePackMajor: Integer;
class var FServicePackMinor: Integer;
class constructor Create;
public
class function Check(AMajor: Integer): Boolean; overload; static; inline;
class function Check(AMajor, AMinor: Integer): Boolean;
overload; static; inline;
class function Check(AMajor, AMinor, AServicePackMajor: Integer)
: Boolean; overload; static; inline;
class function ToString: string; static;
class property Architecture: TArchitecture read FArchitecture;
class property Build: Integer read FBuild;
class property Major: Integer read FMajor;
class property Minor: Integer read FMinor;
class property Name: string read FName;
class property Platform: TPlatform read FPlatform;
class property ServicePackMajor: Integer read FServicePackMajor;
class property ServicePackMinor: Integer read FServicePackMinor;
end;
You can see right away that this class is exactly what we have been looking for! Not only can we determine the version and its architecture but also the operating system. That means you can use this function in the VCL and FireMonkey just the same.
I have to emphasize that the class overwrites ToString. In order to provide your users with a nice and readable info string about the operating system, simply use TOSVersion.ToString
. Mind, that you do not have to instantiate the class as an object. Use the name of the class as all properties and functions are defined for the class.
Finally, there is no need to compare version numbers in your code. Use the Check
functions that are provided to check for certain version numbers etc.