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;overload
staticinlineclass
function
Check(AMajor, AMinor: Integer): Boolean;overload
staticinlineclass
function
Check(AMajor, AMinor, AServicePackMajor: Integer): Boolean;overload
staticinlineclass
function
ToString:string
staticclass
property
Architecture: TArchitectureread
FArchitecture;
class
property
Build: Integerread
FBuild;
class
property
Major: Integerread
FMajor;
class
property
Minor: Integerread
FMinor;
class
property
Name:string
read
FName;
class
property
Platform
: TPlatformread
FPlatform;
class
property
ServicePackMajor: Integerread
FServicePackMajor;
class
property
ServicePackMinor: Integerread
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.Linesdo
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
aof
arIntelX86: Result :='IntelX86'
arIntelX64: Result :='IntelX64'
else
Result :='UNKNOWN OS architecture'
end
end
function
OSPlatformToStr(const
p: TOSVersion.TPlatform):string
begin
case
pof
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