sysutils.inc 19 KB

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