Delphi check OS version

Building apps for multiple operating systems from the very same source code
imposes unique challenges. Your code might be running on iOS or Android.
It only takes two mouse clicks to recompile your app for a different target.
Certain features might exist on a given platform and not be there on other
platforms. Going further, new functionality is constantly being added to
platforms, so we may want to know on which platform and operating
system version your app is being executed.

This can be done with the TOSVersion record type defined in the
System.SysUtils unit. It has a class constructor that instantiates all its fields.
The TOSVersion record type has inner TArchitecture and TPlatform
enumerated types and corresponding public class properties to read the
current operating system architecture, platform, name, and major and minor
numbers. Additionally, we can check major and minor numbers of the service
pack level.

Here’s some code:


uses
 System.SysUtils;

type
TOSArchitectureHelper = record helper for TOSVersion.TArchitecture
 function ToString: string;
end;

TOSPlatformHelper = record helper for TOSVersion.TPlatform
 function ToString: string;
end;

function OSArchToStr(const Value: TOSVersion.TArchitecture): string;
function OSPlatToStr(const Value: TOSVersion.TPlatform): string;

implementation

function OSArchToStr(const Value: TOSVersion.TArchitecture): string;
begin
 case Value of
  arIntelX86: Result := 'IntelX86';
  arIntelX64: Result := 'IntelX64';
  arARM32: Result := 'ARM32';
  arARM64: Result := 'ARM64';
  else Result := 'Unknown OS Architecture';
 end;
end;

function TOSArchitectureHelper.ToString: string;
begin
 Result := OSArchToStr(self);
end;

function OSPlatToStr(const Value: TOSVersion.TPlatform): string;
begin
 case Value of
  pfWindows: Result := 'Windows';
  pfMacOS: Result := 'MacOS';
  pfiOS: Result := 'iOS';
  pfAndroid: Result := 'Android';
  pfWinRT: Result := 'WinRT';
  pfLinux: Result := 'Linux';
  else Result := 'Unknown OS Platform'
 end;
end;

function TOSPlatformHelper.ToString: string;
begin
 Result := OSPlatToStr(self);
end;

Add this unit to the uses clause of the main form. We can additionally define
in the form class a simple Log(s: string) method that will just display a
given string in the memo.

323 risposte a “Delphi check OS version”

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.