New "TOSVersion" type in Delphi XE2

by Aug 31, 2011

Delphi XE2 is probably the most significant release since Delphi 1.  The number of new features is amazing and probably the biggest, most important one is FireMonkey – a brand new business application development platform that let you use Delphi or C++ to write applications that natively run on Windows and Mac OS.

If you are writing an application that is designed to run on Windows and Mac it can be very useful to be able to programmatically check what operation system the application is running against.

Delphi XE2 adds new "TOSVersion" record type to "System.SysUtils" unit that can be used to retrieve information about platform, architecture and version numbers of the underlying OS.

type
TOSVersion = record
public type
TArchitecture = (arIntelX86, arIntelX64);
TPlatform = (pfWindows, pfMacOS);
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; overloadstaticinline class function Check(AMajor, AMinor: Integer): Boolean; overloadstaticinline class function Check(AMajor, AMinor, AServicePackMajor: Integer): Boolean; overloadstaticinline class function ToString: stringstatic 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

The "TOSVerison" is a record type, so there is no need to instantiate it before accessing its members.

Note the "TArchitecture" and "TPlatform" enumerated types are nested within "TOSVersion" type. I like this approach, where it is obvious where these two enumerations logically belong.

Hey! Let's give this new type a try!

1. Create a new "Delphi FireMonkey HD Application"

2. Press "Ctrl + ." to bring up "IDE Insight". Start typing "TPanel" and add it to the form. Set its "Align" property to "alTop", so it is aligned on top of the form.

3. Add "TMemo" to the form and align it to "alClient".

4. Add a "TButton" to the panel, double-click it and enter the following code into the "OnClick" event handler:

uses
uOSVersionUtils;

procedure TFormOSVersion.ButtonGetOSInfoClick(Sender: TObject);
begin
with MemoLog.Lines do
begin
Clear;
Add(TOSVersion.ToString);
Add('' Add('Architecture: ' + OSArchitectureToStr(TOSVersion.Architecture));
Add('Platform: ' + OSPlatformToStr(TOSVersion.Platform) + IntToStr(PlatformFromPointer));
Add('Build: ' + IntToStr(TOSVersion.Build));
Add('Major: ' + IntToStr(TOSVersion.Major));
Add('Minor: ' + IntToStr(TOSVersion.Minor));
Add('Name: ' + TOSVersion.Name);
Add('Service Pack - Major: ' + IntToStr(TOSVersion.ServicePackMajor));
Add('Service Pack - Minor: ' + IntToStr(TOSVersion.ServicePackMinor));
end end

5. As the last step add a new empty Delphi unit to the application, save it as "uOSVersionUtils" and replace its content with the following code that contains two utility functions to convert "TArchitecture" and "TPlatform" values to strings:

unit uOSVersionUtils;

interface

uses
System.SysUtils;

function OSArchitectureToStr(const a: TOSVersion.TArchitecture): string function OSPlatformToStr(const p: TOSVersion.TPlatform): string function PlatformFromPointer: integer;

implementation

function OSArchitectureToStr(const a: TOSVersion.TArchitecture): string begin
case a of
arIntelX86: Result := 'IntelX86' arIntelX64: Result := 'IntelX64' else
Result := 'UNKNOWN OS architecture' end end
function OSPlatformToStr(const p: TOSVersion.TPlatform): string begin
case p of
pfWindows: Result := 'Windows' pfMacOS: Result := 'MacOS' else
Result := 'UNKNOWN OS Platform' end end
function PlatformFromPointer: integer;
begin
Result := SizeOf(Pointer) * 8;
end
end.

When I run this application on Windows, after clicking the button, it looks like this:

When I run it on Mac OS it looks like this:

Using the methods and properties of the new Delphi XE2 "TOSVersion" you can make decisions in your code depending on the version of the underlying operating system your FireMonkey application is running against. That's powerful stuff!

Delphi and FireMonkey programming is real fun!

The source code of the "OSVersionApp" demo is available for download from Embarcadero Code Central: http://cc.embarcadero.com/item/28481