Quick.SysInfo.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.SysInfo
  4. Description : System Info functions
  5. Author : Kike Pérez
  6. Version : 1.2
  7. Created : 17/05/2018
  8. Modified : 11/09/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.SysInfo;
  22. {$i QuickLib.inc}
  23. interface
  24. {$IFDEF FPC}
  25. {$modeSwitch advancedRecords}
  26. {$ENDIF}
  27. uses
  28. SysUtils,
  29. {$IFNDEF FPC}
  30. {$IFDEF NEXTGEN}
  31. System.IOUtils,
  32. {$IFDEF ANDROID}
  33. Androidapi.Helpers,
  34. {$ENDIF}
  35. {$IFDEF IOS}
  36. Macapi.CoreFoundation,
  37. iOSApi.Foundation,
  38. {$ENDIF}
  39. {$ENDIF}
  40. {$ENDIF}
  41. Quick.Commons;
  42. type
  43. TSystemInfo = record
  44. private
  45. fAppName : string;
  46. fAppVersion : string;
  47. fAppPath : string;
  48. fHostName : string;
  49. fUserName : string;
  50. fOSVersion : string;
  51. fCPUCores : Integer;
  52. function GetOSVersion : string;
  53. public
  54. procedure GetInfo;
  55. property AppName : string read fAppName write fAppName;
  56. property AppVersion : string read fAppVersion write fAppVersion;
  57. property AppPath : string read fAppPath write fAppPath;
  58. property HostName : string read fHostName write fHostName;
  59. property UserName : string read fUserName write fUserName;
  60. property OsVersion : string read fOSVersion write fOSVersion;
  61. property CPUCores : Integer read fCPUCores write fCPUCores;
  62. end;
  63. var
  64. SystemInfo : TSystemInfo;
  65. implementation
  66. { TSystemInfo }
  67. procedure TSystemInfo.GetInfo;
  68. begin
  69. {$IFNDEF NEXTGEN}
  70. if IsLibrary then fAppName := ExtractFileNameWithoutExt(GetModuleName(0))
  71. else fAppName := ExtractFilenameWithoutExt(ParamStr(0));
  72. {$ELSE}
  73. {$IFDEF ANDROID}
  74. fAppName := JStringToString(SharedActivityContext.getPackageName);
  75. {$ELSE}
  76. fAppName := TNSString.Wrap(CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle, kCFBundleIdentifierKey)).UTF8String;
  77. {$ENDIF}
  78. {$ENDIF}
  79. fAppVersion := GetAppVersionFullStr;
  80. {$IFNDEF NEXTGEN}
  81. if IsLibrary then fAppPath := ExtractFilePath(GetModuleName(0))
  82. else fAppPath := ExtractFilePath(ParamStr(0));
  83. {$ELSE}
  84. fAppPath := TPath.GetDocumentsPath;
  85. {$ENDIf}
  86. {$IFDEF DELPHILINUX}
  87. fUserName := String.Copy(GetLoggedUserName);
  88. {$ELSE}
  89. fUserName := Trim(GetLoggedUserName);
  90. {$ENDIF}
  91. fHostName := GetComputerName;
  92. fOSVersion := GetOSVersion;
  93. fCPUCores := CPUCount;
  94. end;
  95. function TSystemInfo.GetOSVersion: string;
  96. begin
  97. Result := {$IFDEF FPC}
  98. {$I %FPCTARGETOS%}+'-'+{$I %FPCTARGETCPU%}
  99. {$ELSE}
  100. TOSVersion.ToString
  101. {$ENDIF};
  102. end;
  103. initialization
  104. try
  105. SystemInfo.GetInfo;
  106. except
  107. {$IFDEF MSWINDOWS}
  108. on E : Exception do
  109. begin
  110. if (not IsLibrary) and (not IsService) then raise Exception.CreateFmt('Error getting SystemInfo: %s',[e.Message]);
  111. end;
  112. {$ENDIF}
  113. end;
  114. end.