Author: Tóth Erik
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
type</code><br> TOSVersion = <code>record</code><br> public</code> <code>type</code><br> TArchitecture = (arIntelX86, arIntelX64);<br> TPlatform = (pfWindows, pfMacOS);<br> private</code><br> class</code> <code>var</code> FArchitecture: TArchitecture;<br> class</code> <code>var</code> FBuild: Integer;<br> class</code> <code>var</code> FMajor: Integer;<br> class</code> <code>var</code> FMinor: Integer;<br> class</code> <code>var</code> FName: <code>string</code>;<br> class</code> <code>var</code> FPlatform: TPlatform;<br> class</code> <code>var</code> FServicePackMajor: Integer;<br> class</code> <code>var</code> FServicePackMinor: Integer;<br> class</code> <code>constructor</code> Create;<br> public</code><br> class</code> <code>function</code> Check(AMajor: Integer): Boolean; <code>overload</code>; <code>static</code>; <code>inline</code>;<br> class</code> <code>function</code> Check(AMajor, AMinor: Integer): Boolean; <code>overload</code>; <code>static</code>; <code>inline</code>;<br> class</code> <code>function</code> Check(AMajor, AMinor, AServicePackMajor: Integer): Boolean; <code>overload</code>; <code>static</code>; <code>inline</code>;<br> class</code> <code>function</code> ToString: <code>string</code>; <code>static</code>;<br> class</code> <code>property</code> Architecture: TArchitecture <code>read</code> FArchitecture;<br> class</code> <code>property</code> Build: Integer <code>read</code> FBuild;<br> class</code> <code>property</code> Major: Integer <code>read</code> FMajor;<br> class</code> <code>property</code> Minor: Integer <code>read</code> FMinor;<br> class</code> <code>property</code> Name: <code>string</code> <code>read</code> FName;<br> class</code> <code>property</code> <code>Platform</code>: TPlatform <code>read</code> FPlatform;<br> class</code> <code>property</code> ServicePackMajor: Integer <code>read</code> FServicePackMajor;<br> class</code> <code>property</code> ServicePackMinor: Integer <code>read</code> FServicePackMinor;<br> end</code>; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
uses</code><br> uOSVersionUtils;<br> <br> procedure</code> TFormOSVersion.ButtonGetOSInfoClick(Sender: TObject);<br> begin</code><br> with</code> MemoLog.Lines <code>do</code><br> begin</code><br> Clear;<br> Add(TOSVersion.ToString);<br> Add(<code>''</code>);<br> Add(<code>'Architecture: '</code> + OSArchitectureToStr(TOSVersion.Architecture));<br> Add(<code>'Platform: '</code> + OSPlatformToStr(TOSVersion.<code>Platform</code>) + IntToStr(PlatformFromPointer));<br> Add(<code>'Build: '</code> + IntToStr(TOSVersion.Build));<br> Add(<code>'Major: '</code> + IntToStr(TOSVersion.Major));<br> Add(<code>'Minor: '</code> + IntToStr(TOSVersion.Minor));<br> Add(<code>'Name: '</code> + TOSVersion.Name);<br> Add(<code>'Service Pack - Major: '</code> + IntToStr(TOSVersion.ServicePackMajor));<br> Add(<code>'Service Pack - Minor: '</code> + IntToStr(TOSVersion.ServicePackMinor));<br> end</code>;<br> end</code>; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
unit</code> uOSVersionUtils;<br> <br> interface</code><br> <br> uses</code><br> System.SysUtils;<br> <br> function</code> OSArchitectureToStr(<code>const</code> a: TOSVersion.TArchitecture): <code>string</code>;<br> function</code> OSPlatformToStr(<code>const</code> p: TOSVersion.TPlatform): <code>string</code>;<br> function</code> PlatformFromPointer: integer;<br> <br> implementation</code><br> <br> function</code> OSArchitectureToStr(<code>const</code> a: TOSVersion.TArchitecture): <code>string</code>;<br> begin</code><br> case</code> a <code>of</code><br> arIntelX86: Result := <code>'IntelX86'</code>;<br> arIntelX64: Result := <code>'IntelX64'</code>;<br> else</code><br> Result := <code>'UNKNOWN OS architecture'</code>;<br> end</code>;<br> end</code>;<br> <br> function</code> OSPlatformToStr(<code>const</code> p: TOSVersion.TPlatform): <code>string</code>;<br> begin</code><br> case</code> p <code>of</code><br> pfWindows: Result := <code>'Windows'</code>;<br> pfMacOS: Result := <code>'MacOS'</code>;<br> else</code><br> Result := <code>'UNKNOWN OS Platform'</code>;<br> end</code>;<br> end</code>;<br> <br> function</code> PlatformFromPointer: integer;<br> begin</code><br> Result := SizeOf(Pointer) * 8;<br> end</code>;<br> <br> end</code>. |
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
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition