sysutils.inc 19 KB

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