Quick.SysInfo.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 : 05/12/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. Types,
  30. {$IFDEF MSWINDOWS}
  31. Windows,
  32. {$ENDIF}
  33. {$IFNDEF FPC}
  34. {$IFDEF NEXTGEN}
  35. System.IOUtils,
  36. {$IFDEF ANDROID}
  37. Androidapi.Helpers,
  38. {$ENDIF}
  39. {$IFDEF IOS}
  40. Macapi.CoreFoundation,
  41. iOSApi.Foundation,
  42. {$ENDIF}
  43. {$ENDIF}
  44. {$ENDIF}
  45. Quick.Commons;
  46. type
  47. TSystemInfo = record
  48. private
  49. fAppName : string;
  50. fAppVersion : string;
  51. fAppPath : string;
  52. fHostName : string;
  53. fUserName : string;
  54. fOSVersion : string;
  55. fCPUCores : Integer;
  56. fProcessId : DWORD;
  57. function GetOSVersion : string;
  58. public
  59. procedure GetInfo;
  60. property AppName : string read fAppName;
  61. property AppVersion : string read fAppVersion;
  62. property AppPath : string read fAppPath;
  63. property HostName : string read fHostName;
  64. property UserName : string read fUserName;
  65. property OsVersion : string read fOSVersion;
  66. property CPUCores : Integer read fCPUCores;
  67. property ProcessId : DWORD read fProcessId;
  68. end;
  69. var
  70. SystemInfo : TSystemInfo;
  71. implementation
  72. { TSystemInfo }
  73. procedure TSystemInfo.GetInfo;
  74. begin
  75. {$IFNDEF NEXTGEN}
  76. if IsLibrary then fAppName := ExtractFileNameWithoutExt(GetModuleName(0))
  77. else fAppName := ExtractFilenameWithoutExt(ParamStr(0));
  78. {$ELSE}
  79. {$IFDEF ANDROID}
  80. fAppName := JStringToString(SharedActivityContext.getPackageName);
  81. {$ELSE}
  82. fAppName := TNSString.Wrap(CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle, kCFBundleIdentifierKey)).UTF8String;
  83. {$ENDIF}
  84. {$ENDIF}
  85. fAppVersion := GetAppVersionFullStr;
  86. {$IFNDEF NEXTGEN}
  87. if IsLibrary then fAppPath := ExtractFilePath(GetModuleName(0))
  88. else fAppPath := ExtractFilePath(ParamStr(0));
  89. {$ELSE}
  90. fAppPath := TPath.GetDocumentsPath;
  91. {$ENDIf}
  92. {$IFDEF DELPHILINUX}
  93. fUserName := GetLoggedUserName;
  94. {$ELSE}
  95. fUserName := Trim(GetLoggedUserName);
  96. {$ENDIF}
  97. fHostName := GetComputerName;
  98. fOSVersion := GetOSVersion;
  99. fCPUCores := CPUCount;
  100. {$IFDEF MSWINDOWS}
  101. fProcessId := GetCurrentProcessID;
  102. {$ELSE}
  103. {$ENDIF}
  104. end;
  105. function TSystemInfo.GetOSVersion: string;
  106. begin
  107. Result := {$IFDEF FPC}
  108. {$I %FPCTARGETOS%}+'-'+{$I %FPCTARGETCPU%}
  109. {$ELSE}
  110. TOSVersion.ToString
  111. {$ENDIF};
  112. end;
  113. initialization
  114. try
  115. SystemInfo.GetInfo;
  116. except
  117. {$IFDEF MSWINDOWS}
  118. on E : Exception do
  119. begin
  120. if (not IsLibrary) and (not IsService) then raise Exception.CreateFmt('Error getting SystemInfo: %s',[e.Message]);
  121. end;
  122. {$ENDIF}
  123. end;
  124. end.