Recent

Author Topic: Reading Platform & Version  (Read 9599 times)

J-G

  • Hero Member
  • *****
  • Posts: 953
Reading Platform & Version
« on: August 24, 2016, 04:58:24 pm »
Sending my early programs to friends for comment and discussion I discovered all manner of issues about cross-platform compiling and 32 v 64 bit options and even how to turn off debugging info in Laz.

I now want to make it a first event in any program to find out what OS, Version, Platform, screen size etc. so need to 'read' this info. I've already found out how to get screen W and H but haven't found the 'key' for O/S etc.

I have found reference to TOSVersion in a Delphi document but can't get that to tell me anything in Laz.

This is such a basic requirement that it must have been asked here many times but after 3 days of searching I haven't found it.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Handoko

  • Hero Member
  • *****
  • Posts: 5151
  • My goal: build my own game engine using Lazarus
Re: Reading Platform & Version
« Reply #1 on: August 24, 2016, 05:20:06 pm »
I have found reference to TOSVersion in a Delphi document but can't get that to tell me anything in Laz.

Perhaps this thread can help:
http://forum.lazarus.freepascal.org/index.php/topic,32847.msg212040.html

RWC

  • Jr. Member
  • **
  • Posts: 92
Re: Reading Platform & Version
« Reply #2 on: August 24, 2016, 05:54:29 pm »
@J-G: Someone? was it GetMem? posted a function to return OS + WidgetSet about a month ago - end July? I can't find it in the search but I did copy the method at the time. Not sure if it helps your case but may help others to repeat it here. see original author's code below. all the best RWC.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. function OSVersion: String;
  32. var sCPU, sOs, sWidgetSet: string;
  33. begin
  34.  sOs:='OTHER';
  35.  sCPU:='???';
  36.  sWidgetSet:='';
  37.  {$IFDEF UNIX              }    // --UNIX--
  38.    {$IFDEF LINUX           }
  39.       sOs:='LINUX';
  40.    {$ENDIF}
  41.    {$IFDEF BSD             }    // --BSD--
  42.      {$IFDEF FREEBSD       }    // FREEBSD
  43.        sOs:='FREEBSD';
  44.      {$ENDIF}
  45.      {$IFDEF NETBSD        }    // NETBSD
  46.        sOs:='NETBSD';
  47.      {$ENDIF}
  48.      {$IFDEF DARWIN        }    // Macintosh
  49.        sOs:='MAC';
  50.      {$ENDIF}
  51.    {$ENDIF}
  52.  {$ENDIF}
  53.  {$IFDEF WINDOWS       }        // --WINDOWS--
  54.    sOs:='WIN';
  55.  {$ENDIF}
  56.  
  57.   //CPU
  58.  {$IFDEF CPUI386}
  59.    sCPU:='x86';
  60.  {$ENDIF}
  61.  {$IFDEF CPUX86_64}
  62.    sCPU:='x64';
  63.  {$ENDIF}
  64.  {$IFDEF CPUARM}
  65.    sCPU:='-ARM';
  66.  {$ENDIF}
  67.  
  68.  //WIDGETSET
  69.  {$IFDEF LCLQt}
  70.   sWidgetSet:='Qt';
  71.  {$ENDIF}
  72.  {$IFDEF LCLGTK}
  73.   sWidgetSet:='Gtk2';
  74.  {$ENDIF}
  75.  {$IFDEF LCLGTK2}
  76.   sWidgetSet:='Gtk2';
  77.  {$ENDIF}
  78.  {$IFDEF LCLGTK3}
  79.   sWidgetSet:='Gtk3';
  80.  {$ENDIF}
  81.  {$IFDEF LCLWin32}
  82.   sWidgetSet:='Win';
  83.  {$ENDIF}
  84.  {$IFDEF LCLCocoa}
  85.  sWidgetSet:='Cocoa';
  86.  {$ENDIF}
  87.  {$IFDEF LCLCarbon}
  88.   sWidgetSet:='Carbon';
  89.  {$ENDIF}
  90.  Result:=sOS+sCPU+' ('+sWidgetSet+')';
  91. end;
  92.  
  93. { TForm1 }
  94.  
  95. procedure TForm1.Button1Click(Sender: TObject);
  96. begin
  97.   Edit1.Text:=OSVersion;
  98. end;
  99.  
  100. end.
  101.  
LAZARUS  : Lazarus-1.4.2-fpc-2.6.4-win32. OS   : Windows Vista 32bit Home Premium SP2.
CPU  : Intel Core2 Quad CPU Q6600 2.4GHz. RAM : 3GB. PCIE : NVIDIA GeForce GT610. Audo : NVIDIA HD Audio.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Reading Platform & Version
« Reply #3 on: August 24, 2016, 06:13:45 pm »
@RWC:
My condolences with that code.

Although the author must have had all the good intentions, it fails miserably. I would say: use the documentation luke.

balazsszekely

  • Guest
Re: Reading Platform & Version
« Reply #4 on: August 24, 2016, 06:20:45 pm »
Quote
Quote
@RWC
@J-G: Someone? was it GetMem? posted a function to return OS + WidgetSet about a month ago - end July?
The forum's search function works very well:
http://forum.lazarus.freepascal.org/index.php/topic,33542.0.html

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading Platform & Version
« Reply #5 on: August 24, 2016, 07:43:56 pm »
Quote
Quote
@RWC
@J-G: Someone? was it GetMem? posted a function to return OS + WidgetSet about a month ago - end July?
The forum's search function works very well:
http://forum.lazarus.freepascal.org/index.php/topic,33542.0.html

I'm sure it does if you ask it the correct question  :)  -  I'm still struggling with deciding what I actually want to know  - but thanks for the pointer GetMem, much appreciated.

Since Handoko was the first to reply I looked at that thread and devised a quick procedure to simply return the version (which is essentially all I want to know for now):
Code: Pascal  [Select][+][-]
  1. Procedure get_Win_Ver;
  2. begin
  3.   O_S := ((Lo(DosVersion)*10)+(Hi(DosVersion))) * 0.1;
  4. end;
  5.  
... and took on board Thaddy's comments.

By the time I'd done that there were 3 more replies and skalogryz's one line solution does all I want.
Not only that, I was able to understand what it did and how to call it!!

What I have to do now is work out how to use the information.  My ideal would be to call this in the .lpr immediately before  ' Application.Run;' and then make a decision whether to continue or abort with a message.

I have been able to put the function in the .lpr and it does report the information but as yet I can see no way to pass a message to the screen. Is it as simple as setting up a component - such as a TLable - in the .lpr and making the TLabel.caption a message?

This leads me to ask another simple question :-  Am I missing something really silly or is the only way to write to the screen via a TLable.caption  (or other component .text or whatever). Sorry if these questions seem very basic but dragging my rusty brain from a DOS environment is no easy task!! 
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Handoko

  • Hero Member
  • *****
  • Posts: 5151
  • My goal: build my own game engine using Lazarus
Re: Reading Platform & Version
« Reply #6 on: August 24, 2016, 08:07:52 pm »
Try this below.

On your mainform OnCreate even put this code:

Code: [Select]
procedure TfrmMain.FormCreate(Sender: TObject);
begin
  if (O_S < 60.1) then begin
    ShowMessage('This program needs at least Windows 7 to run properly.');
    Application.Terminate;
  end;
end;

Although I haven't test the code above, but it should work.
« Last Edit: August 24, 2016, 08:12:08 pm by Handoko »

RWC

  • Jr. Member
  • **
  • Posts: 92
Re: Reading Platform & Version
« Reply #7 on: August 24, 2016, 08:12:37 pm »
Sorry GetMem I mixed this up with one of your earlier replies. My brain function is the problem not the forum's search function. :D Thank you Molly, that post didn't have your input & skalogryz's solution when I copied it.
LAZARUS  : Lazarus-1.4.2-fpc-2.6.4-win32. OS   : Windows Vista 32bit Home Premium SP2.
CPU  : Intel Core2 Quad CPU Q6600 2.4GHz. RAM : 3GB. PCIE : NVIDIA GeForce GT610. Audo : NVIDIA HD Audio.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading Platform & Version
« Reply #8 on: August 24, 2016, 11:49:44 pm »
Thanks for your input Handoko. I've implemented a modified version of your suggestion since I'm using skalogryz's function to read the OSVersion rather than the DOSVersion call.

I've obviously misunderstood something since the gdb tells me that ShowMessage doesn't exist (or words to that effect) and compile fails. This is where I put the modified code :

Code: Pascal  [Select][+][-]
  1. var S : String;
  2.     P : byte;
  3.  
  4. function OSVersion: String;
  5. begin
  6.   Result:={$I %FPCTARGETOS%}+'-'+{$I %FPCTARGETCPU%}+' ('+WidgetSet.ClassName+')';
  7. end;
  8.  
  9. begin
  10.   RequireDerivedFormResource:=True;
  11.   Application.Initialize;
  12.   Application.CreateForm(TEllipse_Form, Ellipse_Form);
  13.   S:= OSVersion;
  14.   p := pos('64',S);
  15.   if p>0 then
  16.     Application.Run
  17.   else
  18.     begin
  19. {      ShowMessage('This program is 64bit but your system is 32bit.');  }
  20.       Application.Terminate;
  21.     end;
  22. end.
  23.  

I've commented out the message line just to make it compile and stepped through correctly proving that '64' is found at p=4 as you would expect on a 64bit system - I'm assuming it would fail on a 32bit but I haven't tried it yet as I don't have Laz installed on my 32bit system.

I presume that I've made a schoolboy error but having less than 3 weeks experience in the Laz environment that's hardly surprising !  I'd be grateful for further specific pointers as to where my error lies  -  or more correctly, how to put it right!

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Reading Platform & Version
« Reply #9 on: August 25, 2016, 12:02:01 am »
@J-G:
Always try to show all the code.

You're most probably missing unit dialogs in your uses clause. see also here.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Reading Platform & Version
« Reply #10 on: August 25, 2016, 02:00:49 am »
Thanks Molly - sorted!

Two 'schoolboy' errors - not showing all the code being the second but at least you knew what was happening.

Dialogs was declared in the units of the .lpi but not in the .lpr and I hadn't put 2 and 2 together to think of the two sections as individuals - -  I said I was 'rusty', I hadn't realized just how rusty!

Now can you tell me if I can make a variable 'global' so that it can be initialized in the .lpr and then seen in the .lpi ?  If it helps the missing code, above the Var declarations, is :
Code: Pascal  [Select][+][-]
  1. program Ellipse;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   dialogs,
  10.   InterfaceBase,
  11.   Interfaces, // this includes the LCL widgetset
  12.   Forms, Ellipse_2
  13.   { you can add units after this };
  14.  
  15. {$R *.res}
  16.  
  17. var S : String;
  18.  
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Reading Platform & Version
« Reply #11 on: August 25, 2016, 03:01:52 am »
Now can you tell me if I can make a variable 'global' so that it can be initialized in the .lpr and then seen in the .lpi ?  If it helps the missing code, above the Var declarations, is :
Well, it is (again) going to be a bit of a long-shot as it is not exactly clear for which units this variable needs to be global.

My best guess would be that you declare variable S  in the interface section of unit Ellipse_2.

In case you need that variable to be 'accessible' in/for other units as well _and_ you end up with a circular reference (the compiler will tell you during compilation), then simply create a new unit (does not have to be a unit that contains a form) and declare the variable there in it's interface section. For each other unit that requires access to that variable: add this unit to the uses clause (can be in the interface _or_ implementation section) of the unit that needs to access the variable.
« Last Edit: August 25, 2016, 03:03:54 am by molly »

PStechPaul

  • Jr. Member
  • **
  • Posts: 76
    • P S Technology, Inc.
Re: Reading Platform & Version
« Reply #12 on: August 25, 2016, 07:33:30 am »
I found the following code in a serial port component ComDrv32. I wasn't able to install it in Lazarus but that was probably due to other problems.

Code: Pascal  [Select][+][-]
  1. function GetWinPlatform: string;
  2. var ov: TOSVERSIONINFO;
  3. begin
  4.   ov.dwOSVersionInfoSize := sizeof(ov);
  5.   if GetVersionEx( ov ) then
  6.   begin
  7.     case ov.dwPlatformId of
  8.       VER_PLATFORM_WIN32s: // Win32s on Windows 3.1
  9.         Result := 'W32S';
  10.       VER_PLATFORM_WIN32_WINDOWS: // Win32 on Windows 95/98
  11.         Result := 'W95';
  12.       VER_PLATFORM_WIN32_NT: // Windows NT
  13.         Result := 'WNT';
  14.     end;
  15.   end
  16.   else
  17.     Result := '??';
  18. end;
  19.  
  20. function GetWinVersion: DWORD;
  21. var ov: TOSVERSIONINFO;
  22. begin
  23.   ov.dwOSVersionInfoSize := sizeof(ov);
  24.   if GetVersionEx( ov ) then
  25.     Result := MAKELONG( ov.dwMinorVersion, ov.dwMajorVersion )
  26.   else
  27.     Result := $00000000;
  28. end;


balazsszekely

  • Guest
Re: Reading Platform & Version
« Reply #13 on: August 25, 2016, 08:00:46 am »
@PStechPaul

If you add unit windows to the uses clauses the code will compile but is incomplete(even for windows). Take a look at this instead:
http://stackoverflow.com/questions/3447536/how-to-determine-the-os
« Last Edit: August 25, 2016, 08:04:35 am by GetMem »

PStechPaul

  • Jr. Member
  • **
  • Posts: 76
    • P S Technology, Inc.
Re: Reading Platform & Version
« Reply #14 on: August 25, 2016, 09:54:27 am »
Yes, I see that it is very old, and incomplete. Somewhere I had a list of all version numbers from Windows 1.0 to 10. I searched and found the following which looks fairly complete (and complex):

http://www.codeproject.com/Articles/73000/Getting-Operating-System-Version-Info-Even-for-Win

More info:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724834%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx

http://codeverge.com/embarcadero.delphi.general/os-version/1060605

The following was posted in the forum above, but it doesn't look quite right:
Code: Pascal  [Select][+][-]
  1. // Get OS info
  2. GetWindowsOS(strTmp);
  3.  
  4. function GetWindowsOS(var strWindowsOS: String) : Boolean;
  5. var
  6.  osVersionInfo: TOSVERSIONINFO;
  7.  strOSVersion: String;
  8.  bIsWindowsXP, bUnknownOS: Boolean;
  9. begin
  10.  // Get the Windows OS version
  11.  { Following taken from MSDN (and duplicates removed)
  12.   Windows 7      6.1
  13.   Windows Vista     6.0
  14.   Windows Server 2003    5.2
  15.   Windows XP      5.1
  16.   Windows 2000     5.0 }
  17.  osVersionInfo.dwOSVersionInfoSize := SizeOf(osVersionInfo);
  18.  GetVersionEx(osVersionInfo);
  19.  
  20.  bIsWindowsXP := False;
  21.  bUnknownOS := False;
  22.  if (osVersionInfo.dwMajorVersion &lt; 5) then
  23.   bUnknownOS := True
  24.  else if (osVersionInfo.dwMajorVersion = 5) then
  25.   begin
  26.   if (osVersionInfo.dwMinorVersion = 0) then
  27.    strOSVersion := &#39;Windows 2000&#39;
  28.   else if (osVersionInfo.dwMinorVersion = 1) then
  29.    begin
  30.    strOSVersion := &#39;Windows XP&#39;;
  31.    if (osVersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT) then
  32.     bIsWindowsXP := True;
  33.    end
  34.   else if (osVersionInfo.dwMinorVersion = 2) then
  35.    strOSVersion := &#39;Windows Server 2003&#39;
  36.   else
  37.    bUnknownOS := True;
  38.   end
  39.  else if (osVersionInfo.dwMajorVersion = 6) then
  40.   begin
  41.   if (osVersionInfo.dwMinorVersion = 0) then
  42.    strOSVersion := &#39;Windows Vista&#39;
  43.   else if (osVersionInfo.dwMinorVersion = 1) then
  44.    strOSVersion := &#39;Windows 7&#39;
  45.   else
  46.    bUnknownOS := True;
  47.   end
  48.  else if (osVersionInfo.dwMajorVersion &gt; 6) then
  49.   bUnknownOS := True;
  50.  
  51.  // OS not recognised?
  52.  if (bUnknownOS) then
  53.   strOSVersion := Format(&#39;Unknown OS (%d.%d)&#39;, [
  54.    osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion]);
  55.  
  56.  // Add the build number and additional service pack information
  57.  strOSVersion := (strOSVersion + Format(&#39;, Build %d, %s&#39;, [
  58.   osVersionInfo.dwBuildNumber, osVersionInfo.szCSDVersion]));
  59.  
  60.  // Set the variable parameter and result
  61.  strWindowsOS := strOSVersion;
  62.  Result := bIsWindowsXP;
  63. end;
« Last Edit: August 25, 2016, 10:02:09 am by PStechPaul »

 

TinyPortal © 2005-2018