sysutils.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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: Integer);
  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: Integer);
  164. begin
  165. inherited create;
  166. fmessage:=Format(Msg,args);
  167. fhelpcontext:=AHelpContext;
  168. end;
  169. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  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: Integer);
  177. begin
  178. inherited create;
  179. fmessage:=Format(ResString^,args);
  180. fhelpcontext:=AHelpContext;
  181. end;
  182. procedure EHeapMemoryError.FreeInstance;
  183. begin
  184. if AllowFree then
  185. inherited FreeInstance;
  186. end;
  187. Constructor EVariantError.CreateCode (Code : longint);
  188. begin
  189. case Code of
  190. VAR_OK:
  191. Create(SNoError);
  192. VAR_PARAMNOTFOUND:
  193. Create(SVarParamNotFound);
  194. VAR_TYPEMISMATCH:
  195. Create(SInvalidVarCast);
  196. VAR_BADVARTYPE:
  197. Create(SVarBadType);
  198. VAR_OVERFLOW:
  199. Create(SVarOverflow);
  200. VAR_BADINDEX:
  201. Create(SVarArrayBounds);
  202. VAR_ARRAYISLOCKED:
  203. Create(SVarArrayLocked);
  204. VAR_NOTIMPL:
  205. Create(SVarNotImplemented);
  206. VAR_OUTOFMEMORY:
  207. Create(SVarOutOfMemory);
  208. VAR_INVALIDARG:
  209. Create(SVarInvalid);
  210. VAR_UNEXPECTED,
  211. VAR_EXCEPTION:
  212. Create(SVarUnexpected);
  213. else
  214. CreateFmt(SUnknownErrorCode,[Code]);
  215. end;
  216. ErrCode:=Code;
  217. end;
  218. {$ifdef windows}
  219. function EExternal.GetExceptionRecord: PExceptionRecord;
  220. begin
  221. result:=@FExceptionRecord;
  222. end;
  223. {$endif windows}
  224. {$push}
  225. {$S-}
  226. Procedure CatchUnhandledException (Obj : TObject; Addr: CodePointer; FrameCount: Longint; Frames: PCodePointer);[public,alias:'FPC_BREAK_UNHANDLED_EXCEPTION'];
  227. Var
  228. i : longint;
  229. hstdout : ^text;
  230. begin
  231. hstdout:=@stdout;
  232. {$if defined(CPUI8086) and (defined(FPC_MM_MEDIUM) or defined(FPC_MM_LARGE) or defined(FPC_MM_HUGE))}
  233. {$else}
  234. Writeln(hstdout^,'An unhandled exception occurred at $',HexStr(Addr),':');
  235. {$endif}
  236. if Obj is exception then
  237. Writeln(hstdout^,Obj.ClassName,': ',Exception(Obj).Message)
  238. else
  239. Writeln(hstdout^,'Exception object ',Obj.ClassName,' is not of class Exception.');
  240. Writeln(hstdout^,BackTraceStrFunc(Addr));
  241. if (FrameCount>0) then
  242. begin
  243. for i:=0 to FrameCount-1 do
  244. Writeln(hstdout^,BackTraceStrFunc(Frames[i]));
  245. end;
  246. Writeln(hstdout^,'');
  247. end;
  248. type
  249. PExceptMapEntry=^TExceptMapEntry;
  250. TExceptMapEntry=record
  251. code: byte;
  252. cls: ExceptClass;
  253. {$IFDEF FPC_HAS_FEATURE_RESOURCES} // This is necessary for 2.4.4, which does not have reasources as a separate feature
  254. msg: PResStringRec;
  255. {$else FPC_HAS_FEATURE_RESOURCES}
  256. msg: PString;
  257. {$endif FPC_HAS_FEATURE_RESOURCES}
  258. end;
  259. const
  260. exceptmap: array[0..28] of TExceptMapEntry = (
  261. (code: 200; cls: EDivByZero; msg: @SDivByZero),
  262. (code: 201; cls: ERangeError; msg: @SRangeError),
  263. (code: 202; cls: EStackOverflow; msg: @SStackOverflow),
  264. (code: 205; cls: EOverflow; msg: @SOverflow),
  265. (code: 206; cls: EUnderflow; msg: @SUnderflow),
  266. (code: 207; cls: EInvalidOp; msg: @SInvalidOp),
  267. { Delphi distinguishes reDivByZero from reZeroDivide, but maps both to code 200. }
  268. (code: 208; cls: EZeroDivide; msg: @SZeroDivide),
  269. (code: 210; cls: EObjectCheck; msg: @SObjectCheckError),
  270. (code: 211; cls: EAbstractError; msg: @SAbstractError),
  271. (code: 212; cls: EExternalException; msg: @SExternalException),
  272. (code: 214; cls: EBusError; msg: @SBusError),
  273. (code: 215; cls: EIntOverflow; msg: @SIntOverflow),
  274. (code: 216; cls: EAccessViolation; msg: @SAccessViolation),
  275. (code: 217; cls: EControlC; msg: @SControlC),
  276. (code: 218; cls: EPrivilege; msg: @SPrivilege),
  277. (code: 219; cls: EInvalidCast; msg: @SInvalidCast),
  278. (code: 220; cls: EVariantError; msg: @SInvalidVarCast),
  279. (code: 221; cls: EVariantError; msg: @SInvalidVarOp),
  280. (code: 222; cls: EVariantError; msg: @SDispatchError),
  281. (code: 223; cls: EVariantError; msg: @SVarArrayCreate),
  282. (code: 224; cls: EVariantError; msg: @SVarNotArray),
  283. (code: 225; cls: EVariantError; msg: @SVarArrayBounds),
  284. (code: 227; cls: EAssertionFailed; msg: @SAssertionFailed),
  285. (code: 228; cls: EIntfCastError; msg: @SIntfCastError),
  286. (code: 229; cls: ESafecallException; msg: @SSafecallException),
  287. (code: 231; cls: EConvertError; msg: @SiconvError),
  288. (code: 232; cls: ENoThreadSupport; msg: @SNoThreadSupport),
  289. (code: 233; cls: ENoWideStringSupport; msg: @SSigQuit),
  290. (code: 234; cls: ENoWideStringSupport; msg: @SMissingWStringManager)
  291. );
  292. function FindExceptMapEntry(err: longint): PExceptMapEntry;
  293. var
  294. i: longint;
  295. begin
  296. for i:=low(exceptmap) to high(exceptmap) do
  297. if err=exceptmap[i].code then
  298. begin
  299. result:=@exceptmap[i];
  300. exit;
  301. end;
  302. result:=nil;
  303. end;
  304. Var OutOfMemory : EOutOfMemory;
  305. InValidPointer : EInvalidPointer;
  306. Procedure RunErrorToExcept (ErrNo : Longint; Address : CodePointer; Frame : Pointer);
  307. var
  308. E: Exception;
  309. HS: PString;
  310. Entry: PExceptMapEntry;
  311. begin
  312. Case Errno of
  313. 1,203 : E:=OutOfMemory;
  314. 204 : E:=InvalidPointer;
  315. else
  316. Entry:=FindExceptMapEntry(ErrNo);
  317. if Assigned(Entry) then
  318. E:=Entry^.cls.CreateRes(Entry^.msg)
  319. else
  320. begin
  321. HS:=nil;
  322. Case Errno of
  323. 2 : HS:=@SFileNotFound;
  324. 3 : HS:=@SInvalidFileName;
  325. 4 : HS:=@STooManyOpenFiles;
  326. 5 : HS:=@SAccessDenied;
  327. 6 : HS:=@SInvalidFileHandle;
  328. 15 : HS:=@SInvalidDrive;
  329. 100 : HS:=@SEndOfFile;
  330. 101 : HS:=@SDiskFull;
  331. 102 : HS:=@SFileNotAssigned;
  332. 103 : HS:=@SFileNotOpen;
  333. 104 : HS:=@SFileNotOpenForInput;
  334. 105 : HS:=@SFileNotOpenForOutput;
  335. 106 : HS:=@SInvalidInput;
  336. end;
  337. if Assigned(HS) then
  338. E:=EInOutError.CreateRes(HS)
  339. else
  340. E:=EInOutError.CreateResFmt(@SUnknownRunTimeError,[errno]);
  341. // this routine can be called from FPC_IOCHECK,
  342. // which clears inoutres and then passes its
  343. // original value to HandleErrorFrame() (which calls
  344. // us). So use errno rather than IOResult, and clear
  345. // InOutRes explicitly in case we can also be called
  346. // from a place that does not clear InOutRes explicitly
  347. EInoutError(E).ErrorCode:=errno;
  348. inoutres:=0;
  349. end;
  350. end;
  351. Raise E at Address,Frame;
  352. end;
  353. {$IFDEF HAS_OSERROR}
  354. Procedure RaiseLastOSError;overload;
  355. begin
  356. RaiseLastOSError(GetLastOSError);
  357. end;
  358. Procedure RaiseLastOSError(LastError: Integer);overload;
  359. var
  360. E : EOSError;
  361. begin
  362. If (LastError<>0) then
  363. E:=EOSError.CreateFmt(SOSError, [LastError, SysErrorMessage(LastError)])
  364. else
  365. E:=EOSError.Create(SUnkOSError);
  366. E.ErrorCode:=LastError;
  367. Raise E;
  368. end;
  369. {$else}
  370. Procedure RaiseLastOSError;overload;
  371. begin
  372. Raise Exception.Create('RaiseLastOSError not implemented on this platform.');
  373. end;
  374. Procedure RaiseLastOSError(LastError: Integer);overload;
  375. begin
  376. RaiseLastOSError;
  377. end;
  378. {$endif}
  379. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo:longint; TheAddr : pointer);
  380. Var
  381. S : String;
  382. begin
  383. If Msg='' then
  384. S:=SAssertionFailed
  385. else
  386. S:=Msg;
  387. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]) at get_caller_addr(theAddr), get_caller_frame(theAddr);
  388. end;
  389. {$pop} //{$S-} for Error handling functions
  390. Procedure InitExceptions;
  391. {
  392. Must install uncaught exception handler (ExceptProc)
  393. and install exceptions for system exceptions or signals.
  394. (e.g: SIGSEGV -> ESegFault or so.)
  395. }
  396. begin
  397. ExceptionClass := Exception;
  398. ExceptProc:=@CatchUnhandledException;
  399. // Create objects that may have problems when there is no memory.
  400. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  401. OutOfMemory.AllowFree:=false;
  402. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  403. InvalidPointer.AllowFree:=false;
  404. AssertErrorProc:=@AssertErrorHandler;
  405. ErrorProc:=@RunErrorToExcept;
  406. OnShowException:=Nil;
  407. end;
  408. Procedure DoneExceptions;
  409. begin
  410. OutOfMemory.AllowFree:=true;
  411. OutOfMemory.Free;
  412. InValidPointer.AllowFree:=true;
  413. InValidPointer.Free;
  414. end;
  415. { Exception handling routines }
  416. function ExceptObject: TObject;
  417. begin
  418. If RaiseList=Nil then
  419. Result:=Nil
  420. else
  421. Result:=RaiseList^.FObject;
  422. end;
  423. function ExceptAddr: CodePointer;
  424. begin
  425. If RaiseList=Nil then
  426. Result:=Nil
  427. else
  428. Result:=RaiseList^.Addr;
  429. end;
  430. function ExceptFrameCount: Longint;
  431. begin
  432. If RaiseList=Nil then
  433. Result:=0
  434. else
  435. Result:=RaiseList^.Framecount;
  436. end;
  437. function ExceptFrames: PCodePointer;
  438. begin
  439. If RaiseList=Nil then
  440. Result:=Nil
  441. else
  442. Result:=RaiseList^.Frames;
  443. end;
  444. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  445. Buffer: PChar; Size: Integer): Integer;
  446. Var
  447. S : AnsiString;
  448. Len : Integer;
  449. begin
  450. S:=Format(SExceptionErrorMessage,[ExceptAddr,ExceptObject.ClassName]);
  451. If ExceptObject is Exception then
  452. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  453. Len:=Length(S);
  454. If S[Len]<>'.' then
  455. begin
  456. S:=S+'.';
  457. Inc(len);
  458. end;
  459. If Len>Size then
  460. Len:=Size;
  461. if Len > 0 then
  462. Move(S[1],Buffer^,Len);
  463. Result:=Len;
  464. end;
  465. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  466. // use shortstring. On exception, the heap may be corrupt.
  467. Var
  468. Buf : ShortString;
  469. begin
  470. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  471. If IsConsole Then
  472. writeln(Buf)
  473. else
  474. If Assigned(OnShowException) Then
  475. OnShowException (Buf);
  476. end;
  477. procedure Abort;
  478. begin
  479. Raise EAbort.Create(SAbortError) at CodePointer(Get_Caller_addr(Get_Frame));
  480. end;
  481. procedure OutOfMemoryError;
  482. begin
  483. Raise OutOfMemory;
  484. end;
  485. { ---------------------------------------------------------------------
  486. Initialization/Finalization/exit code
  487. ---------------------------------------------------------------------}
  488. Type
  489. PPRecord = ^TPRecord;
  490. TPRecord = Record
  491. Func : TTerminateProc;
  492. NextFunc : PPRecord;
  493. end;
  494. Const
  495. TPList : PPRecord = Nil;
  496. procedure AddTerminateProc(TermProc: TTerminateProc);
  497. Var
  498. TPR : PPRecord;
  499. begin
  500. New(TPR);
  501. With TPR^ do
  502. begin
  503. NextFunc:=TPList;
  504. Func:=TermProc;
  505. end;
  506. TPList:=TPR;
  507. end;
  508. function CallTerminateProcs: Boolean;
  509. Var
  510. TPR : PPRecord;
  511. begin
  512. Result:=True;
  513. TPR:=TPList;
  514. While Result and (TPR<>Nil) do
  515. begin
  516. Result:=TPR^.Func();
  517. TPR:=TPR^.NextFunc;
  518. end;
  519. end;
  520. { ---------------------------------------------------------------------
  521. Diskh functions, OS independent.
  522. ---------------------------------------------------------------------}
  523. Function GetCurrentDir: {$ifdef FPC_UNICODE_RTL}UnicodeString{$else}AnsiString{$endif};
  524. begin
  525. GetDir(0,Result);
  526. end;
  527. { ---------------------------------------------------------------------
  528. Other functions, OS independent.
  529. ---------------------------------------------------------------------}
  530. Var
  531. GUIDCalledRandomize : Boolean = False;
  532. Procedure GetRandomBytes(Var Buf; NBytes : Integer);
  533. Var
  534. I : Integer;
  535. P : PByte;
  536. begin
  537. P:=@Buf;
  538. If Not GUIDCalledRandomize then
  539. begin
  540. Randomize;
  541. GUIDCalledRandomize:=True;
  542. end;
  543. For I:=0 to NBytes-1 do
  544. P[i]:=Random(256);
  545. end;
  546. {$IFDEF HASCREATEGUID}
  547. Function SysCreateGUID(out GUID : TGUID) : Integer; forward;
  548. {$ENDIF}
  549. Function CreateGUID(out GUID : TGUID) : Integer;
  550. begin
  551. If Assigned(OnCreateGUID) then
  552. Result:=OnCreateGUID(GUID)
  553. else
  554. begin
  555. {$IFDEF HASCREATEGUID}
  556. Result:=SysCreateGUID(GUID);
  557. {$ELSE}
  558. GetRandomBytes(GUID,SizeOf(Guid));
  559. guid.clock_seq_hi_and_reserved:=(guid.clock_seq_hi_and_reserved and $3F) + 64;
  560. guid.time_hi_and_version :=(guid.time_hi_and_version and $0FFF)+ $4000;
  561. Result:=0;
  562. {$ENDIF}
  563. end;
  564. end;
  565. function SafeLoadLibrary(const FileName: AnsiString;
  566. ErrorMode: DWord = {$ifdef windows}SEM_NOOPENFILEERRORBOX{$else windows}0{$endif windows}): HMODULE;
  567. {$if defined(cpui386) or defined(cpux86_64)}
  568. var
  569. mode : DWord;
  570. fpucw : Word;
  571. ssecw : DWord;
  572. {$endif}
  573. begin
  574. {$if defined(win64) or defined(win32)}
  575. mode:=SetErrorMode(ErrorMode);
  576. {$endif}
  577. try
  578. {$if defined(cpui386) or defined(cpux86_64)}
  579. fpucw:=Get8087CW;
  580. {$ifdef cpui386}
  581. if has_sse_support then
  582. {$endif cpui386}
  583. ssecw:=GetSSECSR;
  584. {$endif}
  585. {$if defined(windows) or defined(win32)}
  586. Result:=LoadLibraryA(PChar(Filename));
  587. {$else}
  588. Result:=0;
  589. {$endif}
  590. finally
  591. {$if defined(cpui386) or defined(cpux86_64)}
  592. Set8087CW(fpucw);
  593. {$ifdef cpui386}
  594. if has_sse_support then
  595. {$endif cpui386}
  596. SetSSECSR(ssecw);
  597. {$endif}
  598. {$if defined(win64) or defined(win32)}
  599. SetErrorMode(mode);
  600. {$endif}
  601. end;
  602. end;
  603. function GetModuleName(Module: HMODULE): string;
  604. begin
  605. {$ifdef MSWINDOWS}
  606. SetLength(Result,MAX_PATH);
  607. SetLength(Result,GetModuleFileNameA(Module, Pchar(Result),Length(Result)));
  608. {$ELSE}
  609. Result:='';
  610. {$ENDIF}
  611. end;
  612. { Beep support }
  613. procedure Beep;
  614. begin
  615. If Assigned(OnBeep) then
  616. OnBeep;
  617. end;