2
0

sysutilh.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. { !!!! }
  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. EOSError = class(Exception)
  170. public
  171. ErrorCode: Longint;
  172. end;
  173. ESafecallException = class(Exception);
  174. ENoThreadSupport = Class(Exception);
  175. ENoWideStringSupport = Class(Exception);
  176. ENoDynLibsSupport = class(Exception);
  177. EProgrammerNotFound = class(Exception);
  178. ENotImplemented = class(Exception);
  179. EArgumentException = class(Exception);
  180. EArgumentOutOfRangeException = class(EArgumentException);
  181. EArgumentNilException = class(EArgumentException);
  182. EPathTooLongException = class(Exception);
  183. ENotSupportedException = class(Exception);
  184. EDirectoryNotFoundException = class(Exception);
  185. EFileNotFoundException = class(Exception);
  186. EPathNotFoundException = class(Exception);
  187. EInvalidOpException = class(Exception);
  188. ENoConstructException = class(Exception);
  189. { Exception handling routines }
  190. function ExceptObject: TObject;
  191. function ExceptAddr: CodePointer;
  192. function ExceptFrameCount: Longint;
  193. function ExceptFrames: PCodePointer;
  194. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  195. Buffer: PChar; Size: Integer): Integer;
  196. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  197. procedure Abort;
  198. procedure OutOfMemoryError;
  199. Type
  200. TBeepHandler = Procedure;
  201. Var
  202. OnBeep : TBeephandler = Nil;
  203. procedure Beep;
  204. function SysErrorMessage(ErrorCode: Integer): String;
  205. Type
  206. TCreateGUIDFunc = Function(Out GUID : TGUID) : Integer;
  207. Var
  208. OnCreateGUID : TCreateGUIDFunc = Nil;
  209. Function CreateGUID(out GUID : TGUID) : Integer;
  210. type
  211. TTerminateProc = Function: Boolean;
  212. procedure AddTerminateProc(TermProc: TTerminateProc);
  213. function CallTerminateProcs: Boolean;
  214. {$IFNDEF VER3_0}
  215. generic function IfThen<T>(val:boolean;const iftrue:T; const iffalse:T) :T; inline; overload;
  216. {$ENDIF}
  217. Var
  218. OnShowException : Procedure (Msg : ShortString);
  219. Const
  220. HexDisplayPrefix : string = '$';
  221. const
  222. // commenting is VP fix. These idents are in a different unit there.
  223. PathDelim={System.}DirectorySeparator;
  224. DriveDelim={System.}DriveSeparator;
  225. PathSep={System.}PathSeparator;
  226. MAX_PATH={System.}MaxPathLen;
  227. Type
  228. TFileRec=FileRec;
  229. TTextRec=TextRec;
  230. { Read pchar handling functions declaration }
  231. {$i syspchh.inc}
  232. { MCBS functions }
  233. {$i sysansih.inc}
  234. {$i syscodepagesh.inc}
  235. { wide string functions }
  236. {$i syswideh.inc}
  237. {$ifdef FPC_HAS_UNICODESTRING}
  238. { unicode string functions }
  239. {$i sysunih.inc}
  240. {$i sysencodingh.inc}
  241. {$endif FPC_HAS_UNICODESTRING}
  242. {$macro on}
  243. {$define PathStr:=UnicodeString}
  244. {$define PathPChar:=PWideChar}
  245. { Read filename handling functions declaration }
  246. {$i finah.inc}
  247. {$define PathStr:=RawByteString}
  248. {$define PathPChar:=PAnsiChar}
  249. { Read filename handling functions declaration }
  250. {$i finah.inc}
  251. {$undef PathStr}
  252. {$undef PathPChar}
  253. { Read other file handling function declarations }
  254. {$i filutilh.inc}
  255. { Read disk function declarations }
  256. {$i diskh.inc}
  257. { read thread handling }
  258. {$i systhrdh.inc}
  259. { Type Helpers}
  260. {$i syshelph.inc}
  261. procedure FreeAndNil(var obj);
  262. procedure FreeMemAndNil(var p);
  263. { interface handling }
  264. {$i intfh.inc}
  265. function SafeLoadLibrary(const FileName: AnsiString;
  266. ErrorMode: DWord = {$ifdef windows}SEM_NOOPENFILEERRORBOX{$else windows}0{$endif windows}): HMODULE;
  267. function GetModuleName(Module: HMODULE): string;
  268. { some packages and unit related constants for compatibility }
  269. const
  270. pfExeModule = $00000000;
  271. pfNeverBuild = $00000001;
  272. pfDesignOnly = $00000002;
  273. pfRunOnly = $00000004;
  274. pfIgnoreDupUnits = $00000008;
  275. pfPackageModule = $40000000;
  276. pfModuleTypeMask = $C0000000;
  277. pfV3Produced = $00000000;
  278. pfProducerUndefined = $04000000;
  279. pfBCB4Produced = $08000000;
  280. pfDelphi4Produced = $0C000000;
  281. pfLibraryModule = $80000000;
  282. pfProducerMask = $0C000000;
  283. const
  284. ufMainUnit = $01;
  285. ufPackageUnit = $02;
  286. ufWeakUnit = $04;
  287. ufOrgWeakUnit = $08;
  288. ufImplicitUnit = $10;
  289. ufWeakPackageUnit = ufPackageUnit or ufWeakUnit;