2
0

Quick.SysInfo.pas 3.9 KB

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