sysutils.inc 17 KB

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