2
0

sysutils.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. { Read filename handling functions implementation }
  12. {$i fina.inc}
  13. { Read file utility functions implementation }
  14. {$i filutil.inc}
  15. { variant error codes }
  16. {$i varerror.inc}
  17. {$ifndef OS_FILEISREADONLY}
  18. {$ifdef FPC_UNICODE_RTL}
  19. Function FileIsReadOnly(const FileName: RawByteString): Boolean;
  20. {$else}
  21. Function FileIsReadOnly(const FileName: String): Boolean;
  22. {$endif}
  23. begin
  24. Result := (FileGetAttr(FileName) and faReadOnly) <> 0;
  25. end;
  26. {$ifdef FPC_UNICODE_RTL}
  27. Function FileIsReadOnly(const FileName: UnicodeString): Boolean;
  28. begin
  29. Result := (FileGetAttr(FileName) and faReadOnly) <> 0;
  30. end;
  31. {$endif}
  32. {$endif OS_FILEISREADONLY}
  33. {$ifndef OS_FILESETDATEBYNAME}
  34. {$ifdef FPC_UNICODE_RTL}
  35. Function FileSetDate (Const FileName : RawByteString;Age : Longint) : Longint;
  36. {$else}
  37. Function FileSetDate (Const FileName : String;Age : Longint) : Longint;
  38. {$endif}
  39. Var
  40. fd : THandle;
  41. begin
  42. { at least windows requires fmOpenWrite here }
  43. fd:=FileOpen(FileName,fmOpenWrite);
  44. If (Fd<>feInvalidHandle) then
  45. try
  46. Result:=FileSetDate(fd,Age);
  47. finally
  48. FileClose(fd);
  49. end
  50. else
  51. {$ifdef HAS_OSERROR}
  52. Result:=GetLastOSError;
  53. {$else}
  54. Result:=-1;
  55. {$endif}
  56. end;
  57. {$ifdef FPC_UNICODE_RTL}
  58. Function FileSetDate (Const FileName : UnicodeString;Age : Longint) : Longint;
  59. Var
  60. fd : THandle;
  61. begin
  62. { at least windows requires fmOpenWrite here }
  63. fd:=FileOpen(FileName,fmOpenWrite);
  64. If (Fd<>feInvalidHandle) then
  65. try
  66. Result:=FileSetDate(fd,Age);
  67. finally
  68. FileClose(fd);
  69. end
  70. else
  71. {$ifdef HAS_OSERROR}
  72. Result:=GetLastOSError;
  73. {$else}
  74. Result:=-1;
  75. {$endif}
  76. end;
  77. {$endif}
  78. {$endif}
  79. { Read String Handling functions implementation }
  80. {$i sysstr.inc}
  81. { Read date & Time function implementations }
  82. {$ifndef FPUNONE}
  83. {$i dati.inc}
  84. {$endif}
  85. {$IFNDEF HAS_GETTICKCOUNT}
  86. function GetTickCount: LongWord;
  87. begin
  88. Result := LongWord(GetTickCount64);
  89. end;
  90. {$ENDIF}
  91. {$IFNDEF HAS_GETTICKCOUNT64}
  92. function GetTickCount64: QWord;
  93. begin
  94. {$IFDEF FPU_NONE}
  95. {$IFDEF HAS_SYSTIMERTICK}
  96. Result := SysTimerTick;
  97. {$ELSE}
  98. Result := 0;
  99. {$ENDIF}
  100. {$ELSE}
  101. Result := Trunc(Now * 24 * 60 * 60 * 1000);
  102. {$ENDIF}
  103. end;
  104. {$ENDIF}
  105. { Read pchar handling functions implementation }
  106. {$i syspch.inc}
  107. { generic internationalisation code }
  108. {$i sysint.inc}
  109. { MCBS functions }
  110. {$i sysansi.inc}
  111. {$i syscodepages.inc}
  112. { wide string functions }
  113. {$i syswide.inc}
  114. {$ifdef FPC_HAS_UNICODESTRING}
  115. { unicode string functions }
  116. {$i sysuni.inc}
  117. {$i sysencoding.inc}
  118. {$endif FPC_HAS_UNICODESTRING}
  119. { threading stuff }
  120. {$i sysuthrd.inc}
  121. { OS utility code }
  122. {$i osutil.inc}
  123. procedure FreeAndNil(var obj);
  124. var
  125. temp: tobject;
  126. begin
  127. temp:=tobject(obj);
  128. pointer(obj):=nil;
  129. temp.free;
  130. end;
  131. { Interfaces support }
  132. {$i sysuintf.inc}
  133. constructor Exception.Create(const msg : string);
  134. begin
  135. inherited create;
  136. fmessage:=msg;
  137. end;
  138. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  139. begin
  140. inherited create;
  141. fmessage:=Format(msg,args);
  142. end;
  143. constructor Exception.CreateRes(ResString: PString);
  144. begin
  145. inherited create;
  146. fmessage:=ResString^;
  147. end;
  148. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const);
  149. begin
  150. inherited create;
  151. fmessage:=Format(ResString^,args);
  152. end;
  153. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  154. begin
  155. inherited create;
  156. fmessage:=Msg;
  157. fhelpcontext:=AHelpContext;
  158. end;
  159. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  160. AHelpContext: Integer);
  161. begin
  162. inherited create;
  163. fmessage:=Format(Msg,args);
  164. fhelpcontext:=AHelpContext;
  165. end;
  166. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  167. begin
  168. inherited create;
  169. fmessage:=ResString^;
  170. fhelpcontext:=AHelpContext;
  171. end;
  172. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  173. AHelpContext: Integer);
  174. begin
  175. inherited create;
  176. fmessage:=Format(ResString^,args);
  177. fhelpcontext:=AHelpContext;
  178. end;
  179. procedure EHeapMemoryError.FreeInstance;
  180. begin
  181. if AllowFree then
  182. inherited FreeInstance;
  183. end;
  184. Constructor EVariantError.CreateCode (Code : longint);
  185. begin
  186. case Code of
  187. VAR_OK:
  188. Create(SNoError);
  189. VAR_PARAMNOTFOUND:
  190. Create(SVarParamNotFound);
  191. VAR_TYPEMISMATCH:
  192. Create(SInvalidVarCast);
  193. VAR_BADVARTYPE:
  194. Create(SVarBadType);
  195. VAR_OVERFLOW:
  196. Create(SVarOverflow);
  197. VAR_BADINDEX:
  198. Create(SVarArrayBounds);
  199. VAR_ARRAYISLOCKED:
  200. Create(SVarArrayLocked);
  201. VAR_NOTIMPL:
  202. Create(SVarNotImplemented);
  203. VAR_OUTOFMEMORY:
  204. Create(SVarOutOfMemory);
  205. VAR_INVALIDARG:
  206. Create(SVarInvalid);
  207. VAR_UNEXPECTED,
  208. VAR_EXCEPTION:
  209. Create(SVarUnexpected);
  210. else
  211. CreateFmt(SUnknownErrorCode,[Code]);
  212. end;
  213. ErrCode:=Code;
  214. end;
  215. {$ifdef windows}
  216. function EExternal.GetExceptionRecord: PExceptionRecord;
  217. begin
  218. result:=@FExceptionRecord;
  219. end;
  220. {$endif windows}
  221. {$push}
  222. {$S-}
  223. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer; FrameCount: Longint; Frames: PPointer);[public,alias:'FPC_BREAK_UNHANDLED_EXCEPTION'];
  224. Var
  225. i : longint;
  226. hstdout : ^text;
  227. begin
  228. hstdout:=@stdout;
  229. Writeln(hstdout^,'An unhandled exception occurred at $',HexStr(Addr),':');
  230. if Obj is exception then
  231. Writeln(hstdout^,Obj.ClassName,': ',Exception(Obj).Message)
  232. else
  233. Writeln(hstdout^,'Exception object ',Obj.ClassName,' is not of class Exception.');
  234. Writeln(hstdout^,BackTraceStrFunc(Addr));
  235. if (FrameCount>0) then
  236. begin
  237. for i:=0 to FrameCount-1 do
  238. Writeln(hstdout^,BackTraceStrFunc(Frames[i]));
  239. end;
  240. Writeln(hstdout^,'');
  241. end;
  242. type
  243. PExceptMapEntry=^TExceptMapEntry;
  244. TExceptMapEntry=record
  245. code: byte;
  246. cls: ExceptClass;
  247. {$IFDEF FPC_HAS_FEATURE_RESOURCES} // This is necessary for 2.4.4, which does not have reasources as a separate feature
  248. msg: PResStringRec;
  249. {$else FPC_HAS_FEATURE_RESOURCES}
  250. msg: PString;
  251. {$endif FPC_HAS_FEATURE_RESOURCES}
  252. end;
  253. const
  254. exceptmap: array[0..28] of TExceptMapEntry = (
  255. (code: 200; cls: EDivByZero; msg: @SDivByZero),
  256. (code: 201; cls: ERangeError; msg: @SRangeError),
  257. (code: 202; cls: EStackOverflow; msg: @SStackOverflow),
  258. (code: 205; cls: EOverflow; msg: @SOverflow),
  259. (code: 206; cls: EUnderflow; msg: @SUnderflow),
  260. (code: 207; cls: EInvalidOp; msg: @SInvalidOp),
  261. { Delphi distinguishes reDivByZero from reZeroDivide, but maps both to code 200. }
  262. (code: 208; cls: EZeroDivide; msg: @SZeroDivide),
  263. (code: 210; cls: EObjectCheck; msg: @SObjectCheckError),
  264. (code: 211; cls: EAbstractError; msg: @SAbstractError),
  265. (code: 212; cls: EExternalException; msg: @SExternalException),
  266. (code: 214; cls: EBusError; msg: @SBusError),
  267. (code: 215; cls: EIntOverflow; msg: @SIntOverflow),
  268. (code: 216; cls: EAccessViolation; msg: @SAccessViolation),
  269. (code: 217; cls: EControlC; msg: @SControlC),
  270. (code: 218; cls: EPrivilege; msg: @SPrivilege),
  271. (code: 219; cls: EInvalidCast; msg: @SInvalidCast),
  272. (code: 220; cls: EVariantError; msg: @SInvalidVarCast),
  273. (code: 221; cls: EVariantError; msg: @SInvalidVarOp),
  274. (code: 222; cls: EVariantError; msg: @SDispatchError),
  275. (code: 223; cls: EVariantError; msg: @SVarArrayCreate),
  276. (code: 224; cls: EVariantError; msg: @SVarNotArray),
  277. (code: 225; cls: EVariantError; msg: @SVarArrayBounds),
  278. (code: 227; cls: EAssertionFailed; msg: @SAssertionFailed),
  279. (code: 228; cls: EIntfCastError; msg: @SIntfCastError),
  280. (code: 229; cls: ESafecallException; msg: @SSafecallException),
  281. (code: 231; cls: EConvertError; msg: @SiconvError),
  282. (code: 232; cls: ENoThreadSupport; msg: @SNoThreadSupport),
  283. (code: 233; cls: ENoWideStringSupport; msg: @SSigQuit),
  284. (code: 234; cls: ENoWideStringSupport; msg: @SMissingWStringManager)
  285. );
  286. function FindExceptMapEntry(err: longint): PExceptMapEntry;
  287. var
  288. i: longint;
  289. begin
  290. for i:=low(exceptmap) to high(exceptmap) do
  291. if err=exceptmap[i].code then
  292. begin
  293. result:=@exceptmap[i];
  294. exit;
  295. end;
  296. result:=nil;
  297. end;
  298. Var OutOfMemory : EOutOfMemory;
  299. InValidPointer : EInvalidPointer;
  300. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  301. var
  302. E: Exception;
  303. HS: PString;
  304. Entry: PExceptMapEntry;
  305. begin
  306. Case Errno of
  307. 1,203 : E:=OutOfMemory;
  308. 204 : E:=InvalidPointer;
  309. else
  310. Entry:=FindExceptMapEntry(ErrNo);
  311. if Assigned(Entry) then
  312. E:=Entry^.cls.CreateRes(Entry^.msg)
  313. else
  314. begin
  315. HS:=nil;
  316. Case Errno of
  317. 2 : HS:=@SFileNotFound;
  318. 3 : HS:=@SInvalidFileName;
  319. 4 : HS:=@STooManyOpenFiles;
  320. 5 : HS:=@SAccessDenied;
  321. 6 : HS:=@SInvalidFileHandle;
  322. 15 : HS:=@SInvalidDrive;
  323. 100 : HS:=@SEndOfFile;
  324. 101 : HS:=@SDiskFull;
  325. 102 : HS:=@SFileNotAssigned;
  326. 103 : HS:=@SFileNotOpen;
  327. 104 : HS:=@SFileNotOpenForInput;
  328. 105 : HS:=@SFileNotOpenForOutput;
  329. 106 : HS:=@SInvalidInput;
  330. end;
  331. if Assigned(HS) then
  332. E:=EInOutError.CreateRes(HS)
  333. else
  334. E:=EInOutError.CreateResFmt(@SUnknownRunTimeError,[errno]);
  335. // this routine can be called from FPC_IOCHECK,
  336. // which clears inoutres and then passes its
  337. // original value to HandleErrorFrame() (which calls
  338. // us). So use errno rather than IOResult, and clear
  339. // InOutRes explicitly in case we can also be called
  340. // from a place that does not clear InOutRes explicitly
  341. EInoutError(E).ErrorCode:=errno;
  342. inoutres:=0;
  343. end;
  344. end;
  345. Raise E at Address,Frame;
  346. end;
  347. {$IFDEF HAS_OSERROR}
  348. Procedure RaiseLastOSError;overload;
  349. begin
  350. RaiseLastOSError(GetLastOSError);
  351. end;
  352. Procedure RaiseLastOSError(LastError: Integer);overload;
  353. var
  354. E : EOSError;
  355. begin
  356. If (LastError<>0) then
  357. E:=EOSError.CreateFmt(SOSError, [LastError, SysErrorMessage(LastError)])
  358. else
  359. E:=EOSError.Create(SUnkOSError);
  360. E.ErrorCode:=LastError;
  361. Raise E;
  362. end;
  363. {$else}
  364. Procedure RaiseLastOSError;overload;
  365. begin
  366. Raise Exception.Create('RaiseLastOSError not implemented on this platform.');
  367. end;
  368. Procedure RaiseLastOSError(LastError: Integer);overload;
  369. begin
  370. RaiseLastOSError;
  371. end;
  372. {$endif}
  373. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo:longint; TheAddr : pointer);
  374. Var
  375. S : String;
  376. begin
  377. If Msg='' then
  378. S:=SAssertionFailed
  379. else
  380. S:=Msg;
  381. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]) at get_caller_addr(theAddr), get_caller_frame(theAddr);
  382. end;
  383. {$pop} //{$S-} for Error handling functions
  384. Procedure InitExceptions;
  385. {
  386. Must install uncaught exception handler (ExceptProc)
  387. and install exceptions for system exceptions or signals.
  388. (e.g: SIGSEGV -> ESegFault or so.)
  389. }
  390. begin
  391. ExceptionClass := Exception;
  392. ExceptProc:=@CatchUnhandledException;
  393. // Create objects that may have problems when there is no memory.
  394. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  395. OutOfMemory.AllowFree:=false;
  396. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  397. InvalidPointer.AllowFree:=false;
  398. AssertErrorProc:=@AssertErrorHandler;
  399. ErrorProc:=@RunErrorToExcept;
  400. OnShowException:=Nil;
  401. end;
  402. Procedure DoneExceptions;
  403. begin
  404. OutOfMemory.AllowFree:=true;
  405. OutOfMemory.Free;
  406. InValidPointer.AllowFree:=true;
  407. InValidPointer.Free;
  408. end;
  409. { Exception handling routines }
  410. function ExceptObject: TObject;
  411. begin
  412. If RaiseList=Nil then
  413. Result:=Nil
  414. else
  415. Result:=RaiseList^.FObject;
  416. end;
  417. function ExceptAddr: Pointer;
  418. begin
  419. If RaiseList=Nil then
  420. Result:=Nil
  421. else
  422. Result:=RaiseList^.Addr;
  423. end;
  424. function ExceptFrameCount: Longint;
  425. begin
  426. If RaiseList=Nil then
  427. Result:=0
  428. else
  429. Result:=RaiseList^.Framecount;
  430. end;
  431. function ExceptFrames: PPointer;
  432. begin
  433. If RaiseList=Nil then
  434. Result:=Nil
  435. else
  436. Result:=RaiseList^.Frames;
  437. end;
  438. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  439. Buffer: PChar; Size: Integer): Integer;
  440. Var
  441. S : AnsiString;
  442. Len : Integer;
  443. begin
  444. S:=Format(SExceptionErrorMessage,[ExceptAddr,ExceptObject.ClassName]);
  445. If ExceptObject is Exception then
  446. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  447. Len:=Length(S);
  448. If S[Len]<>'.' then
  449. begin
  450. S:=S+'.';
  451. Inc(len);
  452. end;
  453. If Len>Size then
  454. Len:=Size;
  455. if Len > 0 then
  456. Move(S[1],Buffer^,Len);
  457. Result:=Len;
  458. end;
  459. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  460. // use shortstring. On exception, the heap may be corrupt.
  461. Var
  462. Buf : ShortString;
  463. begin
  464. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  465. If IsConsole Then
  466. writeln(Buf)
  467. else
  468. If Assigned(OnShowException) Then
  469. OnShowException (Buf);
  470. end;
  471. procedure Abort;
  472. begin
  473. Raise EAbort.Create(SAbortError) at Pointer(Get_Caller_addr(Get_Frame));
  474. end;
  475. procedure OutOfMemoryError;
  476. begin
  477. Raise OutOfMemory;
  478. end;
  479. { ---------------------------------------------------------------------
  480. Initialization/Finalization/exit code
  481. ---------------------------------------------------------------------}
  482. Type
  483. PPRecord = ^TPRecord;
  484. TPRecord = Record
  485. Func : TTerminateProc;
  486. NextFunc : PPRecord;
  487. end;
  488. Const
  489. TPList : PPRecord = Nil;
  490. procedure AddTerminateProc(TermProc: TTerminateProc);
  491. Var
  492. TPR : PPRecord;
  493. begin
  494. New(TPR);
  495. With TPR^ do
  496. begin
  497. NextFunc:=TPList;
  498. Func:=TermProc;
  499. end;
  500. TPList:=TPR;
  501. end;
  502. function CallTerminateProcs: Boolean;
  503. Var
  504. TPR : PPRecord;
  505. begin
  506. Result:=True;
  507. TPR:=TPList;
  508. While Result and (TPR<>Nil) do
  509. begin
  510. Result:=TPR^.Func();
  511. TPR:=TPR^.NextFunc;
  512. end;
  513. end;
  514. { ---------------------------------------------------------------------
  515. Diskh functions, OS independent.
  516. ---------------------------------------------------------------------}
  517. function ForceDirectories(Const Dir: string): Boolean;
  518. var
  519. E: EInOutError;
  520. ADrv : String;
  521. function DoForceDirectories(Const Dir: string): Boolean;
  522. var
  523. ADir : String;
  524. APath: String;
  525. begin
  526. Result:=True;
  527. ADir:=ExcludeTrailingPathDelimiter(Dir);
  528. if (ADir='') then Exit;
  529. if Not DirectoryExists(ADir) then
  530. begin
  531. APath := ExtractFilePath(ADir);
  532. //this can happen on Windows if user specifies Dir like \user\name/test/
  533. //and would, if not checked for, cause an infinite recusrsion and a stack overflow
  534. if (APath = ADir) then Result := False
  535. else Result:=DoForceDirectories(APath);
  536. If Result then
  537. Result := CreateDir(ADir);
  538. end;
  539. end;
  540. function IsUncDrive(const Drv: String): Boolean;
  541. begin
  542. Result := (Length(Drv) > 2) and (Drv[1] = PathDelim) and (Drv[2] = PathDelim);
  543. end;
  544. begin
  545. Result := False;
  546. ADrv := ExtractFileDrive(Dir);
  547. if (ADrv<>'') and (not DirectoryExists(ADrv))
  548. {$IFNDEF FORCEDIR_NO_UNC_SUPPORT} and (not IsUncDrive(ADrv)){$ENDIF} then Exit;
  549. if Dir='' then
  550. begin
  551. E:=EInOutError.Create(SCannotCreateEmptyDir);
  552. E.ErrorCode:=3;
  553. Raise E;
  554. end;
  555. Result := DoForceDirectories(SetDirSeparators(Dir));
  556. end;
  557. Var
  558. GUIDCalledRandomize : Boolean = False;
  559. Procedure GetRandomBytes(Var Buf; NBytes : Integer);
  560. Var
  561. I : Integer;
  562. P : PByte;
  563. begin
  564. P:=@Buf;
  565. If Not GUIDCalledRandomize then
  566. begin
  567. Randomize;
  568. GUIDCalledRandomize:=True;
  569. end;
  570. For I:=0 to NBytes-1 do
  571. P[i]:=Random(256);
  572. end;
  573. {$IFDEF HASCREATEGUID}
  574. Function SysCreateGUID(out GUID : TGUID) : Integer; forward;
  575. {$ENDIF}
  576. Function CreateGUID(out GUID : TGUID) : Integer;
  577. begin
  578. If Assigned(OnCreateGUID) then
  579. Result:=OnCreateGUID(GUID)
  580. else
  581. begin
  582. {$IFDEF HASCREATEGUID}
  583. Result:=SysCreateGUID(GUID);
  584. {$ELSE}
  585. GetRandomBytes(GUID,SizeOf(Guid));
  586. guid.clock_seq_hi_and_reserved:=(guid.clock_seq_hi_and_reserved and $3F) + 64;
  587. guid.time_hi_and_version :=(guid.time_hi_and_version and $0FFF)+ $4000;
  588. Result:=0;
  589. {$ENDIF}
  590. end;
  591. end;
  592. function SafeLoadLibrary(const FileName: AnsiString;
  593. ErrorMode: DWord = {$ifdef windows}SEM_NOOPENFILEERRORBOX{$else windows}0{$endif windows}): HMODULE;
  594. {$if defined(cpui386) or defined(cpux86_64)}
  595. var
  596. mode : DWord;
  597. fpucw : Word;
  598. ssecw : DWord;
  599. {$endif}
  600. begin
  601. {$if defined(win64) or defined(win32)}
  602. mode:=SetErrorMode(ErrorMode);
  603. {$endif}
  604. try
  605. {$if defined(cpui386) or defined(cpux86_64)}
  606. fpucw:=Get8087CW;
  607. {$ifdef cpui386}
  608. if has_sse_support then
  609. {$endif cpui386}
  610. ssecw:=GetSSECSR;
  611. {$endif}
  612. {$if defined(windows) or defined(win32)}
  613. Result:=LoadLibraryA(PChar(Filename));
  614. {$else}
  615. Result:=0;
  616. {$endif}
  617. finally
  618. {$if defined(cpui386) or defined(cpux86_64)}
  619. Set8087CW(fpucw);
  620. {$ifdef cpui386}
  621. if has_sse_support then
  622. {$endif cpui386}
  623. SetSSECSR(ssecw);
  624. {$endif}
  625. {$if defined(win64) or defined(win32)}
  626. SetErrorMode(mode);
  627. {$endif}
  628. end;
  629. end;
  630. function GetModuleName(Module: HMODULE): string;
  631. begin
  632. {$ifdef MSWINDOWS}
  633. SetLength(Result,MAX_PATH);
  634. SetLength(Result,GetModuleFileNameA(Module, Pchar(Result),Length(Result)));
  635. {$ELSE}
  636. Result:='';
  637. {$ENDIF}
  638. end;
  639. { Beep support }
  640. procedure Beep;
  641. begin
  642. If Assigned(OnBeep) then
  643. OnBeep;
  644. end;