Recent

Author Topic: [SOLVED] Operating System Version String  (Read 8956 times)

Shebuka

  • Sr. Member
  • ****
  • Posts: 422
[SOLVED] Operating System Version String
« on: May 11, 2011, 11:28:41 am »
Hi all,
is there an easy way to get a Mac OS X Version String, like one you see under Software voice in System Profiler (in my case 'Mac OS X 10.6.7 (10J869)' )?

I'v found that i can create a TProcess and execute system_profiler SPSoftwareDataType but it' not really the easiest way...
« Last Edit: July 25, 2011, 03:35:58 pm by Shebuka »

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Operating System Version String
« Reply #1 on: May 11, 2011, 04:36:57 pm »
I'm sure there's a better way, but this works:

'Mac OS X 10.' + IntToStr(Lo(DosVersion) - 4) + '.' + IntToStr(Hi(DosVersion));

Does require the Dos unit, though.

Thanks.

-Phil

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7493
Re: Operating System Version String
« Reply #2 on: May 12, 2011, 09:23:07 am »
Have a look had baseunix.fpuname

Shebuka

  • Sr. Member
  • ****
  • Posts: 422
Re: Operating System Version String
« Reply #3 on: June 30, 2011, 03:47:51 pm »
If anyone need it, this one i made works fine ;)

Code: Pascal  [Select]
  1. function ExtractOSVersionFromProfiler(): string;
  2. var
  3.   ProfProcess: TProcess;
  4.   path: string;
  5.   ProfStringList: TStringList;
  6.   NumRighe, i, HeaderStart, HeaderLength: integer;
  7. begin
  8.   Result := '';
  9.  
  10.   path := 'sh -c "system_profiler SPSoftwareDataType"';
  11.   try
  12.     ProfProcess := Tprocess.Create(nil);
  13.     ProfProcess.CommandLine := path;
  14.     ProfProcess.Options := ProfProcess.Options + [poUsePipes, poWaitOnExit];
  15.     ProfProcess.Execute;
  16.   except
  17.     exit;
  18.   end;
  19.  
  20.   try
  21.     ProfStringList := TStringList.Create;
  22.     ProfStringList.LoadFromStream(ProfProcess.Output);
  23.   except
  24.     exit;
  25.   end;
  26.  
  27.   ProfProcess.Terminate(0);
  28.   FreeAndNil(ProfProcess);
  29.  
  30.   NumRighe := ProfStringList.Count;
  31.  
  32.   for i := 0 to NumRighe - 1 do
  33.   begin
  34.     HeaderStart := AnsiPos('System Version: ', ProfStringList.Strings[i]);
  35.     if HeaderStart > 0 then
  36.     begin
  37.       HeaderLength := Length('System Version: ');
  38.       Result := AnsiMidStr(ProfStringList.Strings[i], HeaderStart + HeaderLength, Length(ProfStringList.Strings[i]));
  39.       Break;
  40.     end;
  41.   end;
  42.  
  43.   FreeAndNil(ProfStringList);
  44. end;

IndianaJones

  • Hero Member
  • *****
  • Posts: 509
Re: [SOLVED] Operating System Version String
« Reply #4 on: November 19, 2011, 07:57:59 pm »

Here is another version.

function ExtractOSVersionFromSW_Vers(Index: Byte): String;
// Index 0: ProductName, 1: ProductVersion, 2: BuildVersion
var
  ProfProcess: TProcess;
  path: string;
  ProfStringList: TStringList;
  Value: string;
begin
  Result := '';
  path := 'sh -c "/usr/bin/sw_vers"';
  try
    ProfProcess := Tprocess.Create(nil);
    ProfProcess.CommandLine := path;
    ProfProcess.Options := ProfProcess.Options + [poUsePipes, poWaitOnExit];
    ProfProcess.Execute;
  except
    exit;
  end;
  try
    ProfStringList := TStringList.Create;
    ProfStringList.LoadFromStream(ProfProcess.Output);
  except
    exit;
  end;

  ProfProcess.Terminate(0);
  ProfStringList.NameValueSeparator:=':';
  Result:=Trim(ProfstringList.ValueFromIndex[Index]);
  FreeAndNil(ProfProcess);FreeAndNil(ProfStringList);
end;

jus

  • New Member
  • *
  • Posts: 14
Re: [SOLVED] Operating System Version String
« Reply #5 on: November 24, 2011, 11:24:08 pm »
Ok, here is another version too. I've already posted this in the German Lazarus forum some times ago. I wrote a dylib-library. You can use the following 2 functions for free.   :)
Code: [Select]
GetMacOSVersion: function(): CFStringRef; cdecl;
GetMacOSVersion2: function(var systemVersionMajor:SInt32; var systemVersionMinor: SInt32; var systemVersionBugFix:SInt32): CFStringRef; cdecl;

GetMacOSVersion ... returns a localised os version string like "Version 10.6.7 (10J869)"
GetMacOSVersion2 ... returns also the OS version info Major, Minor, Bugfix directly as integer values

The following sample application contains only a TButton and a TMemo:
Code: [Select]
unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  dynlibs, MacOSAll, CarbonProc;
 
type
 
  { TForm1 }
 
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
 
var
  Form1: TForm1;
  H : TLibHandle;
 
  GetMacOSVersion: function(): CFStringRef; cdecl;
  GetMacOSVersion2: function(var systemVersionMajor:SInt32; var systemVersionMinor: SInt32; var systemVersionBugFix:SInt32): CFStringRef; cdecl;
 
implementation
 
{$R *.lfm}
 
 
{ TForm1 }
 
procedure TForm1.Button1Click(Sender: TObject);
var
  cfs: CFStringRef;
  s: String;
  systemVersionMajor, systemVersionMinor, systemVersionBugFix: SInt32;
begin
  H := LoadLibrary('/Users/mac/Desktop/getosversion.dylib'); //<------ change here the directory path!!!
  pointer(GetMacOSVersion) := GetProcedureAddress(H, 'GetMacOSVersion');
  pointer(GetMacOSVersion2) := GetProcedureAddress(H, 'GetMacOSVersion2');
 
  cfs:=GetMacOSVersion();
  s:=CFStringToStr(cfs);
  Memo1.Lines.Add('Mac Version (System): '+s);
 
  cfs:=GetMacOSVersion2(systemVersionMajor, systemVersionMinor, systemVersionBugFix);
  s:=CFStringToStr(cfs);
  Memo1.Lines.Add('Mac Version (System): '+s);
  Memo1.Lines.Add('Major: '+IntToStr(systemVersionMajor));
  Memo1.Lines.Add('Minor: '+IntToStr(systemVersionMinor));
  Memo1.Lines.Add('BugFix: '+IntToStr(systemVersionBugFix));
  FreeLibrary(H);
end;
 
end.

As shown above, the USES part should contain "dynlibs, MacOSAll, CarbonProc". If you want to use the sample source above directly, you must change the directory to your real directory in the line of LOADLIBRARY, where the "getosversion.dylib" is located on your harddisk.

jus

P.S.: Sorry for my bad english.  :-[
« Last Edit: November 24, 2011, 11:30:23 pm by jus »