sysutilh.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. { Read String Handling functions declaration }
  22. {$i sysstrh.inc}
  23. type
  24. { some helpful data types }
  25. THandle = System.THandle;
  26. TProcedure = procedure;
  27. TFilename = String;
  28. TIntegerSet = Set of 0..SizeOf(Integer)*8-1;
  29. LongRec = packed record
  30. case Integer of
  31. {$ifdef FPC_LITTLE_ENDIAN}
  32. 0 : (Lo,Hi : Word);
  33. {$else FPC_LITTLE_ENDIAN}
  34. 0 : (Hi,Lo : Word);
  35. {$endif FPC_LITTLE_ENDIAN}
  36. 1 : (Bytes : Array[0..3] of Byte);
  37. end;
  38. WordRec = packed record
  39. {$ifdef FPC_LITTLE_ENDIAN}
  40. Lo,Hi : Byte;
  41. {$else FPC_LITTLE_ENDIAN}
  42. Hi,Lo : Byte;
  43. {$endif FPC_LITTLE_ENDIAN}
  44. end;
  45. Int64Rec = packed record
  46. case integer of
  47. {$ifdef FPC_LITTLE_ENDIAN}
  48. 0 : (Lo,Hi : Cardinal);
  49. {$else FPC_LITTLE_ENDIAN}
  50. 0 : (Hi,Lo : Cardinal);
  51. {$endif FPC_LITTLE_ENDIAN}
  52. 1 : (Words : Array[0..3] of Word);
  53. 2 : (Bytes : Array[0..7] of Byte);
  54. end;
  55. Int128Rec = packed record
  56. case integer of
  57. {$ifdef FPC_LITTLE_ENDIAN}
  58. 0 : (Lo,Hi : QWord);
  59. {$else FPC_LITTLE_ENDIAN}
  60. 0 : (Hi,Lo : QWord);
  61. {$endif FPC_LITTLE_ENDIAN}
  62. 1 : (DWords : Array[0..3] of DWord);
  63. 2 : (Words : Array[0..7] of Word);
  64. 3 : (Bytes : Array[0..15] of Byte);
  65. end;
  66. OWordRec = packed record
  67. case integer of
  68. {$ifdef FPC_LITTLE_ENDIAN}
  69. 0 : (Lo,Hi : QWord);
  70. {$else FPC_LITTLE_ENDIAN}
  71. 0 : (Hi,Lo : QWord);
  72. {$endif FPC_LITTLE_ENDIAN}
  73. 1 : (DWords : Array[0..3] of DWord);
  74. 2 : (Words : Array[0..7] of Word);
  75. 3 : (Bytes : Array[0..15] of Byte);
  76. end;
  77. PByteArray = ^TByteArray;
  78. TByteArray = Array[0..32767] of Byte;
  79. PWordarray = ^TWordArray;
  80. TWordArray = array[0..16383] of Word;
  81. TBytes = array of Byte;
  82. { exceptions }
  83. Exception = class(TObject)
  84. private
  85. fmessage : string;
  86. fhelpcontext : longint;
  87. public
  88. constructor Create(const msg : string);
  89. constructor CreateFmt(const msg : string; const args : array of const);
  90. constructor CreateRes(ResString: PString);
  91. constructor CreateResFmt(ResString: PString; const Args: array of const);
  92. constructor CreateHelp(const Msg: string; AHelpContext: Integer);
  93. constructor CreateFmtHelp(const Msg: string; const Args: array of const;
  94. AHelpContext: Integer);
  95. constructor CreateResHelp(ResString: PString; AHelpContext: Integer);
  96. constructor CreateResFmtHelp(ResString: PString; const Args: array of const;
  97. AHelpContext: Integer);
  98. { !!!! }
  99. property HelpContext : longint read fhelpcontext write fhelpcontext;
  100. property Message : string read fmessage write fmessage;
  101. end;
  102. ExceptClass = class of Exception;
  103. EExternal = class(Exception)
  104. {$ifdef windows}
  105. { OS-provided exception record is stored on stack and has very limited lifetime.
  106. Therefore store a complete copy. }
  107. private
  108. FExceptionRecord: TExceptionRecord;
  109. function GetExceptionRecord: PExceptionRecord;
  110. public
  111. property ExceptionRecord : PExceptionRecord read GetExceptionRecord;
  112. {$endif windows}
  113. end;
  114. { integer math exceptions }
  115. EInterror = Class(EExternal);
  116. EDivByZero = Class(EIntError);
  117. ERangeError = Class(EIntError);
  118. EIntOverflow = Class(EIntError);
  119. { General math errors }
  120. EMathError = Class(EExternal);
  121. EInvalidOp = Class(EMathError);
  122. EZeroDivide = Class(EMathError);
  123. EOverflow = Class(EMathError);
  124. EUnderflow = Class(EMathError);
  125. { Run-time and I/O Errors }
  126. EInOutError = class(Exception)
  127. public
  128. ErrorCode : Longint;
  129. end;
  130. EHeapMemoryError = class(Exception)
  131. protected
  132. AllowFree : boolean;
  133. public
  134. procedure FreeInstance;override;
  135. end;
  136. EHeapException = EHeapMemoryError;
  137. EExternalException = class(EExternal);
  138. EInvalidPointer = Class(EHeapMemoryError);
  139. EOutOfMemory = Class(EHeapMemoryError);
  140. EInvalidCast = Class(Exception);
  141. EVariantError = Class(Exception)
  142. ErrCode : longint;
  143. Constructor CreateCode(Code : Longint);
  144. end;
  145. EAccessViolation = Class(EExternal);
  146. EBusError = Class(EAccessViolation);
  147. EPrivilege = class(EExternal);
  148. EStackOverflow = class(EExternal);
  149. EControlC = class(EExternal);
  150. { String conversion errors }
  151. EConvertError = class(Exception);
  152. EFormatError = class(Exception);
  153. { Other errors }
  154. EAbort = Class(Exception);
  155. EAbstractError = Class(Exception);
  156. EAssertionFailed = Class(Exception);
  157. EPropReadOnly = class(Exception);
  158. EPropWriteOnly = class(Exception);
  159. EIntfCastError = class(Exception);
  160. EInvalidContainer = class(Exception);
  161. EInvalidInsert = class(Exception);
  162. EPackageError = class(Exception);
  163. EOSError = class(Exception)
  164. public
  165. ErrorCode: Longint;
  166. end;
  167. ESafecallException = class(Exception);
  168. ENoThreadSupport = Class(Exception);
  169. ENoWideStringSupport = Class(Exception);
  170. ENotImplemented = class(Exception);
  171. EArgumentException = class(Exception);
  172. EArgumentOutOfRangeException = class(EArgumentException);
  173. ENoConstructException = class(Exception);
  174. { Exception handling routines }
  175. function ExceptObject: TObject;
  176. function ExceptAddr: Pointer;
  177. function ExceptFrameCount: Longint;
  178. function ExceptFrames: PPointer;
  179. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  180. Buffer: PChar; Size: Integer): Integer;
  181. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  182. procedure Abort;
  183. procedure OutOfMemoryError;
  184. Type
  185. TBeepHandler = Procedure;
  186. Var
  187. OnBeep : TBeephandler = Nil;
  188. procedure Beep;
  189. function SysErrorMessage(ErrorCode: Integer): String;
  190. Type
  191. TCreateGUIDFunc = Function(Out GUID : TGUID) : Integer;
  192. Var
  193. OnCreateGUID : TCreateGUIDFunc = Nil;
  194. Function CreateGUID(out GUID : TGUID) : Integer;
  195. type
  196. TTerminateProc = Function: Boolean;
  197. procedure AddTerminateProc(TermProc: TTerminateProc);
  198. function CallTerminateProcs: Boolean;
  199. Var
  200. OnShowException : Procedure (Msg : ShortString);
  201. { FileRec/TextRec }
  202. {$i filerec.inc}
  203. {$i textrec.inc}
  204. Const
  205. HexDisplayPrefix : string = '$';
  206. const
  207. // commenting is VP fix. These idents are in a different unit there.
  208. PathDelim={System.}DirectorySeparator;
  209. DriveDelim={System.}DriveSeparator;
  210. PathSep={System.}PathSeparator;
  211. MAX_PATH={System.}MaxPathLen;
  212. Type
  213. TFileRec=FileRec;
  214. TTextRec=TextRec;
  215. { Read pchar handling functions declaration }
  216. {$i syspchh.inc}
  217. { MCBS functions }
  218. {$i sysansih.inc}
  219. {$i syscodepagesh.inc}
  220. { wide string functions }
  221. {$i syswideh.inc}
  222. {$ifdef FPC_HAS_UNICODESTRING}
  223. { unicode string functions }
  224. {$i sysunih.inc}
  225. {$i sysencodingh.inc}
  226. {$endif FPC_HAS_UNICODESTRING}
  227. { Read filename handling functions declaration }
  228. {$i finah.inc}
  229. { Read other file handling function declarations }
  230. {$i filutilh.inc}
  231. { Read disk function declarations }
  232. {$i diskh.inc}
  233. { read thread handling }
  234. {$i systhrdh.inc}
  235. procedure FreeAndNil(var obj);
  236. { interface handling }
  237. {$i intfh.inc}
  238. function SafeLoadLibrary(const FileName: AnsiString;
  239. ErrorMode: DWord = {$ifdef windows}SEM_NOOPENFILEERRORBOX{$else windows}0{$endif windows}): HMODULE;
  240. function GetModuleName(Module: HMODULE): string;
  241. { some packages and unit related constants for compatibility }
  242. const
  243. pfExeModule = $00000000;
  244. pfNeverBuild = $00000001;
  245. pfDesignOnly = $00000002;
  246. pfRunOnly = $00000004;
  247. pfIgnoreDupUnits = $00000008;
  248. pfPackageModule = $40000000;
  249. pfModuleTypeMask = $C0000000;
  250. pfV3Produced = $00000000;
  251. pfProducerUndefined = $04000000;
  252. pfBCB4Produced = $08000000;
  253. pfDelphi4Produced = $0C000000;
  254. pfLibraryModule = $80000000;
  255. pfProducerMask = $0C000000;
  256. const
  257. ufMainUnit = $01;
  258. ufPackageUnit = $02;
  259. ufWeakUnit = $04;
  260. ufOrgWeakUnit = $08;
  261. ufImplicitUnit = $10;
  262. ufWeakPackageUnit = ufPackageUnit or ufWeakUnit;