sysutilh.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { Using inlining for small system functions/wrappers }
  12. {$inline on}
  13. {$define SYSUTILSINLINE}
  14. { Read internationalization settings }
  15. {$i sysinth.inc}
  16. { Read date & Time function declarations }
  17. {$i osutilsh.inc}
  18. {$ifndef FPUNONE}
  19. {$i datih.inc}
  20. {$endif}
  21. function GetTickCount: LongWord; deprecated 'Use GetTickCount64 instead';
  22. function GetTickCount64: QWord;
  23. { Read String Handling functions declaration }
  24. {$i sysstrh.inc}
  25. type
  26. { some helpful data types }
  27. THandle = System.THandle;
  28. TProcedure = procedure;
  29. TFileName = type string;
  30. TIntegerSet = Set of 0..SizeOf(Integer)*8-1;
  31. LongRec = packed record
  32. case Integer of
  33. {$ifdef FPC_LITTLE_ENDIAN}
  34. 0 : (Lo,Hi : Word);
  35. {$else FPC_LITTLE_ENDIAN}
  36. 0 : (Hi,Lo : Word);
  37. {$endif FPC_LITTLE_ENDIAN}
  38. 1 : (Bytes : Array[0..3] of Byte);
  39. end;
  40. WordRec = packed record
  41. {$ifdef FPC_LITTLE_ENDIAN}
  42. Lo,Hi : Byte;
  43. {$else FPC_LITTLE_ENDIAN}
  44. Hi,Lo : Byte;
  45. {$endif FPC_LITTLE_ENDIAN}
  46. end;
  47. Int64Rec = packed record
  48. case integer of
  49. {$ifdef FPC_LITTLE_ENDIAN}
  50. 0 : (Lo,Hi : Cardinal);
  51. {$else FPC_LITTLE_ENDIAN}
  52. 0 : (Hi,Lo : Cardinal);
  53. {$endif FPC_LITTLE_ENDIAN}
  54. 1 : (Words : Array[0..3] of Word);
  55. 2 : (Bytes : Array[0..7] of Byte);
  56. end;
  57. Int128Rec = packed record
  58. case integer of
  59. {$ifdef FPC_LITTLE_ENDIAN}
  60. 0 : (Lo,Hi : QWord);
  61. {$else FPC_LITTLE_ENDIAN}
  62. 0 : (Hi,Lo : QWord);
  63. {$endif FPC_LITTLE_ENDIAN}
  64. 1 : (DWords : Array[0..3] of DWord);
  65. 2 : (Words : Array[0..7] of Word);
  66. 3 : (Bytes : Array[0..15] of Byte);
  67. end;
  68. OWordRec = packed record
  69. case integer of
  70. {$ifdef FPC_LITTLE_ENDIAN}
  71. 0 : (Lo,Hi : QWord);
  72. {$else FPC_LITTLE_ENDIAN}
  73. 0 : (Hi,Lo : QWord);
  74. {$endif FPC_LITTLE_ENDIAN}
  75. 1 : (DWords : Array[0..3] of DWord);
  76. 2 : (Words : Array[0..7] of Word);
  77. 3 : (Bytes : Array[0..15] of Byte);
  78. end;
  79. PByteArray = ^TByteArray;
  80. TByteArray = Array[0..{$ifdef CPU16}32766{$else}32767{$endif}] of Byte;
  81. PWordarray = ^TWordArray;
  82. TWordArray = array[0..{$ifdef CPU16}16382{$else}16383{$endif}] of Word;
  83. TBytes = array of Byte;
  84. { exceptions }
  85. Exception = class(TObject)
  86. private
  87. fmessage : string;
  88. fhelpcontext : longint;
  89. public
  90. constructor Create(const msg : string);
  91. constructor CreateFmt(const msg : string; const args : array of const);
  92. constructor CreateRes(ResString: PString);
  93. constructor CreateResFmt(ResString: PString; const Args: array of const);
  94. constructor CreateHelp(const Msg: string; AHelpContext: Longint);
  95. constructor CreateFmtHelp(const Msg: string; const Args: array of const;
  96. AHelpContext: Longint);
  97. constructor CreateResHelp(ResString: PString; AHelpContext: Longint);
  98. constructor CreateResFmtHelp(ResString: PString; const Args: array of const;
  99. AHelpContext: Longint);
  100. Function ToString : String; override;
  101. function GetBaseException : Exception;virtual;
  102. property HelpContext : longint read fhelpcontext write fhelpcontext;
  103. property Message : string read fmessage write fmessage;
  104. end;
  105. ExceptClass = class of Exception;
  106. EExternal = class(Exception)
  107. {$if defined(win32) or defined(win64) or defined(wince)}
  108. { OS-provided exception record is stored on stack and has very limited lifetime.
  109. Therefore store a complete copy. }
  110. private
  111. FExceptionRecord: TExceptionRecord;
  112. function GetExceptionRecord: PExceptionRecord;
  113. public
  114. property ExceptionRecord : PExceptionRecord read GetExceptionRecord;
  115. {$endif win32 or win64 or wince}
  116. end;
  117. { integer math exceptions }
  118. EIntError = Class(EExternal);
  119. EDivByZero = Class(EIntError);
  120. ERangeError = Class(EIntError);
  121. EIntOverflow = Class(EIntError);
  122. { General math errors }
  123. EMathError = Class(EExternal);
  124. EInvalidOp = Class(EMathError);
  125. EZeroDivide = Class(EMathError);
  126. EOverflow = Class(EMathError);
  127. EUnderflow = Class(EMathError);
  128. { Run-time and I/O Errors }
  129. EInOutError = class(Exception)
  130. public
  131. ErrorCode : Integer;
  132. end;
  133. EHeapMemoryError = class(Exception)
  134. protected
  135. AllowFree : boolean;
  136. public
  137. procedure FreeInstance;override;
  138. end;
  139. EHeapException = EHeapMemoryError;
  140. EExternalException = class(EExternal);
  141. EInvalidPointer = Class(EHeapMemoryError);
  142. EOutOfMemory = Class(EHeapMemoryError);
  143. EInvalidCast = Class(Exception);
  144. EVariantError = Class(Exception)
  145. ErrCode : longint;
  146. Constructor CreateCode(Code : Longint);
  147. end;
  148. EAccessViolation = Class(EExternal);
  149. EBusError = Class(EAccessViolation);
  150. EPrivilege = class(EExternal);
  151. EStackOverflow = class(EExternal);
  152. EControlC = class(EExternal);
  153. { String conversion errors }
  154. EConvertError = class(Exception);
  155. EFormatError = class(Exception);
  156. { Other errors }
  157. EAbort = Class(Exception);
  158. EAbstractError = Class(Exception);
  159. EAssertionFailed = Class(Exception);
  160. EObjectCheck = Class(Exception);
  161. EThreadError = Class(Exception);
  162. ESigQuit = Class(Exception);
  163. EPropReadOnly = class(Exception);
  164. EPropWriteOnly = class(Exception);
  165. EIntfCastError = class(Exception);
  166. EInvalidContainer = class(Exception);
  167. EInvalidInsert = class(Exception);
  168. EPackageError = class(Exception);
  169. ECFError = class(Exception);
  170. EOSError = class(Exception)
  171. public
  172. ErrorCode: Longint;
  173. end;
  174. ESafecallException = class(Exception);
  175. ENoThreadSupport = Class(Exception);
  176. ENoWideStringSupport = Class(Exception);
  177. ENoDynLibsSupport = class(Exception);
  178. EProgrammerNotFound = class(Exception);
  179. EMonitor = class(Exception);
  180. EMonitorLockException = class(EMonitor);
  181. ENoMonitorSupportException = class(EMonitor);
  182. EObjectDisposed = class(Exception);
  183. ENotImplemented = class(Exception);
  184. EArgumentException = class(Exception);
  185. EArgumentOutOfRangeException = class(EArgumentException);
  186. EArgumentNilException = class(EArgumentException);
  187. EPathTooLongException = class(Exception);
  188. ENotSupportedException = class(Exception);
  189. EDirectoryNotFoundException = class(Exception);
  190. EFileNotFoundException = class(Exception);
  191. EPathNotFoundException = class(Exception);
  192. EInOutArgumentException = class(EArgumentException)
  193. public
  194. Path: string;
  195. constructor Create(const aMsg, aPath: string); overload;
  196. constructor CreateRes(ResStringRec: PResStringRec; const aPath: string); overload;
  197. constructor CreateFmt(const fmt : string; const args : array of const; const aPath : String); overload;
  198. end;
  199. EInvalidOpException = class(Exception);
  200. ENoConstructException = class(Exception);
  201. EListError = Class(Exception);
  202. EOperationCancelled = class(Exception);
  203. { Exception handling routines }
  204. function ExceptObject: TObject;
  205. function ExceptAddr: CodePointer;
  206. function ExceptFrameCount: Longint;
  207. function ExceptFrames: PCodePointer;
  208. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  209. Buffer: PChar; Size: Integer): Integer;
  210. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  211. procedure Abort;
  212. procedure OutOfMemoryError;
  213. procedure ListIndexError(aIndex,aMax: Integer; aObj: TObject);
  214. Type
  215. TBeepHandler = Procedure;
  216. Var
  217. OnBeep : TBeephandler = Nil;
  218. procedure Beep;
  219. function SysErrorMessage(ErrorCode: Integer): String;
  220. Type
  221. TCreateGUIDFunc = Function(Out GUID : TGUID) : Integer;
  222. Var
  223. OnCreateGUID : TCreateGUIDFunc = Nil;
  224. Function CreateGUID(out GUID : TGUID) : Integer;
  225. type
  226. TTerminateProc = Function: Boolean;
  227. procedure AddTerminateProc(TermProc: TTerminateProc);
  228. function CallTerminateProcs: Boolean;
  229. {$IFNDEF VER3_0}
  230. generic function IfThen<T>(val:boolean;const iftrue:T; const iffalse:T) :T; inline; overload;
  231. generic function Exchange<T>(var target:T; const newvalue:T) :T; inline;
  232. {$ENDIF}
  233. Var
  234. OnShowException : Procedure (Msg : ShortString);
  235. Const
  236. HexDisplayPrefix : string = '$';
  237. const
  238. // commenting is VP fix. These idents are in a different unit there.
  239. PathDelim={System.}DirectorySeparator;
  240. DriveDelim={System.}DriveSeparator;
  241. PathSep={System.}PathSeparator;
  242. MAX_PATH={System.}MaxPathLen;
  243. Type
  244. TFileRec=FileRec;
  245. TTextRec=TextRec;
  246. { Read pchar handling functions declaration }
  247. {$i syspchh.inc}
  248. { MCBS functions }
  249. {$i sysansih.inc}
  250. {$i syscodepagesh.inc}
  251. { wide string functions }
  252. {$i syswideh.inc}
  253. {$ifdef FPC_HAS_UNICODESTRING}
  254. { unicode string functions }
  255. {$i sysunih.inc}
  256. {$i sysencodingh.inc}
  257. {$endif FPC_HAS_UNICODESTRING}
  258. {$macro on}
  259. {$define PathStr:=UnicodeString}
  260. {$define PathPChar:=PUnicodeChar}
  261. { Read filename handling functions declaration }
  262. {$i finah.inc}
  263. {$define PathStr:=RawByteString}
  264. {$define PathPChar:=PAnsiChar}
  265. { Read filename handling functions declaration }
  266. {$i finah.inc}
  267. {$undef PathStr}
  268. {$undef PathPChar}
  269. { Read other file handling function declarations }
  270. {$i filutilh.inc}
  271. { Read disk function declarations }
  272. {$i diskh.inc}
  273. { read thread handling }
  274. {$i systhrdh.inc}
  275. { Type Helpers}
  276. {$i syshelph.inc}
  277. procedure FreeAndNil(var obj);
  278. procedure FreeMemAndNil(var p);
  279. { interface handling }
  280. {$i intfh.inc}
  281. function SafeLoadLibrary(const FileName: AnsiString;
  282. ErrorMode: DWord = {$ifdef windows}SEM_NOOPENFILEERRORBOX{$else windows}0{$endif windows}): HMODULE;
  283. function GetModuleName(Module: HMODULE): string;
  284. { some packages and unit related constants for compatibility }
  285. const
  286. pfExeModule = $00000000;
  287. pfNeverBuild = $00000001;
  288. pfDesignOnly = $00000002;
  289. pfRunOnly = $00000004;
  290. pfIgnoreDupUnits = $00000008;
  291. pfPackageModule = $40000000;
  292. pfModuleTypeMask = $C0000000;
  293. pfV3Produced = $00000000;
  294. pfProducerUndefined = $04000000;
  295. pfBCB4Produced = $08000000;
  296. pfDelphi4Produced = $0C000000;
  297. pfLibraryModule = $80000000;
  298. pfProducerMask = $0C000000;
  299. const
  300. ufMainUnit = $01;
  301. ufPackageUnit = $02;
  302. ufWeakUnit = $04;
  303. ufOrgWeakUnit = $08;
  304. ufImplicitUnit = $10;
  305. ufWeakPackageUnit = ufPackageUnit or ufWeakUnit;
  306. Type
  307. TUTF8StringDynArray = Array of UTF8String;
  308. Function ArrayOfConstToStr(Args: array of const ; aSeparator : Char = ','; aQuoteBegin : Char = '"'; aQuoteEnd : Char = '"') : UTF8String;
  309. Function ArrayOfConstToStrArray(Args: array of const) : TUTF8StringDynArray;
  310. { Delphi compatibility }
  311. Type
  312. { TOSVersion }
  313. TOSVersion = record
  314. public type
  315. TArchitecture = (arIntelX86, arIntelX64, arARM32, arARM64, arIntelX16, arXTensa, arAVR, arM68k, arPowerPC, arPower64,
  316. arMips, arMipsel, arMips64, arMips64el, arJVM, arWasm32, arSparc, arSparc64, arRiscV32, arRiscV64, arZ80,
  317. arLoongArch64, arOther);
  318. TPlatform = (pfWindows, pfMacOS, pfiOS, pfAndroid, pfWinRT, pfLinux,
  319. pfGo32v2, pfOS2, pfFreeBSD, pfBeos, pfNetBSD, pfAmiga,
  320. pfAtari, pfSolaris, pfQNX, pfNetware, pfOpenBSD, pfWDosX,
  321. pfPalmos, pfMacOSClassic, pfDarwin, pfEMX, pfWatcom,
  322. pfMorphos, pfNetwLibC, pfWinCE, pfGBA, pfNDS, pfEmbedded,
  323. pfSymbian, pfHaiku, pfIPhoneSim, pfAIX, pfJava,
  324. pfNativeNT, pfMSDos, pfWII, pfAROS, pfDragonFly, pfWin16,
  325. pfFreeRTOS, pfZXSpectrum, pfMSXDOS, pfAmstradCPC,
  326. pfSinclairQL, pfWasi, pfOther);
  327. public const
  328. AllArchitectures = [Low(TArchitecture) .. High(TArchitecture)];
  329. AllPlatforms = [Low(TPlatform) .. High(TPlatform)];
  330. private
  331. class var FFull : string;
  332. class var FArchitecture: TArchitecture;
  333. class var FBuild: Integer;
  334. class var FMajor: Integer;
  335. class var FMinor: Integer;
  336. class var FName: string;
  337. class var FPlatform: TPlatform;
  338. class var FServicePackMajor: Integer;
  339. class var FServicePackMinor: Integer;
  340. {$IFDEF LINUX}
  341. class var FPrettyName: string;
  342. class var FLibCVersionMajor: Integer;
  343. class var FLibCVersionMinor: Integer;
  344. {$ENDIF LINUX}
  345. class constructor Create;
  346. public
  347. class function Check(AMajor: Integer): Boolean; overload; static; inline;
  348. class function Check(AMajor, AMinor: Integer): Boolean; overload; static; inline;
  349. class function Check(AMajor, AMinor, AServicePackMajor: Integer): Boolean; overload; static; inline;
  350. class function ToString: string; static;
  351. class property Architecture: TArchitecture read FArchitecture;
  352. class property Build: Integer read FBuild;
  353. class property Major: Integer read FMajor;
  354. class property Minor: Integer read FMinor;
  355. class property Name: string read FName;
  356. class property Platform: TPlatform read FPlatform;
  357. class property ServicePackMajor: Integer read FServicePackMajor;
  358. class property ServicePackMinor: Integer read FServicePackMinor;
  359. {$IFDEF LINUX}
  360. class property PrettyName: string read FPrettyName;
  361. class property LibCVersionMajor: Integer read FLibCVersionMajor;
  362. class property LibCVersionMinor: Integer read FLibCVersionMinor;
  363. {$ENDIF LINUX}
  364. end;
  365. TArchitectures = set of TOSVersion.TArchitecture;
  366. TPlatforms = set of TOSVersion.TPlatform;
  367. Function GetCompiledArchitecture : TOSVersion.TArchitecture;
  368. Function GetCompiledPlatform : TOSVersion.TPlatform;
  369. {$IFNDEF HAS_INVALIDHANDLE}
  370. const
  371. INVALID_HANDLE_VALUE = DWORD(-1);
  372. {$ENDIF}