sysutils.inc 17 KB

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