sysutils.inc 18 KB

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