sysutils.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. { Read pchar handling functions implementation }
  107. {$i syspch.inc}
  108. { generic internationalisation code }
  109. {$i sysint.inc}
  110. { MCBS functions }
  111. {$i sysansi.inc}
  112. { wide string functions }
  113. {$i syswide.inc}
  114. {$ifdef FPC_HAS_UNICODESTRING}
  115. { unicode string functions }
  116. {$i sysuni.inc}
  117. {$endif FPC_HAS_UNICODESTRING}
  118. { threading stuff }
  119. {$i sysuthrd.inc}
  120. { OS utility code }
  121. {$i osutil.inc}
  122. procedure FreeAndNil(var obj);
  123. var
  124. temp: tobject;
  125. begin
  126. temp:=tobject(obj);
  127. pointer(obj):=nil;
  128. temp.free;
  129. end;
  130. function FileAge(const FileName: string; out FileDateTime: TDateTime; FollowLink: Boolean = True): Boolean;
  131. Var
  132. Info : TSearchRec;
  133. A : Integer;
  134. begin
  135. for A:=1 to Length(FileName) do
  136. If (FileName[A] in ['?','*']) then
  137. Exit(False);
  138. A:=0;
  139. if Not FollowLink then
  140. A:=A or faSymLink;
  141. Result:=FindFirst(FileName,A,Info)=0;
  142. If Result then
  143. FileDateTime:=FileDatetoDateTime (Info.Time);
  144. FindClose(Info);
  145. end;
  146. { Interfaces support }
  147. {$i sysuintf.inc}
  148. constructor Exception.Create(const msg : string);
  149. begin
  150. inherited create;
  151. fmessage:=msg;
  152. end;
  153. constructor Exception.CreateFmt(const msg : string; const args : array of const);
  154. begin
  155. inherited create;
  156. fmessage:=Format(msg,args);
  157. end;
  158. constructor Exception.CreateRes(ResString: PString);
  159. begin
  160. inherited create;
  161. fmessage:=ResString^;
  162. end;
  163. constructor Exception.CreateResFmt(ResString: PString; const Args: array of const);
  164. begin
  165. inherited create;
  166. fmessage:=Format(ResString^,args);
  167. end;
  168. constructor Exception.CreateHelp(const Msg: string; AHelpContext: Integer);
  169. begin
  170. inherited create;
  171. fmessage:=Msg;
  172. fhelpcontext:=AHelpContext;
  173. end;
  174. constructor Exception.CreateFmtHelp(const Msg: string; const Args: array of const;
  175. AHelpContext: Integer);
  176. begin
  177. inherited create;
  178. fmessage:=Format(Msg,args);
  179. fhelpcontext:=AHelpContext;
  180. end;
  181. constructor Exception.CreateResHelp(ResString: PString; AHelpContext: Integer);
  182. begin
  183. inherited create;
  184. fmessage:=ResString^;
  185. fhelpcontext:=AHelpContext;
  186. end;
  187. constructor Exception.CreateResFmtHelp(ResString: PString; const Args: array of const;
  188. AHelpContext: Integer);
  189. begin
  190. inherited create;
  191. fmessage:=Format(ResString^,args);
  192. fhelpcontext:=AHelpContext;
  193. end;
  194. procedure EHeapMemoryError.FreeInstance;
  195. begin
  196. if AllowFree then
  197. inherited FreeInstance;
  198. end;
  199. Constructor EVariantError.CreateCode (Code : longint);
  200. begin
  201. case Code of
  202. VAR_OK:
  203. Create(SNoError);
  204. VAR_PARAMNOTFOUND:
  205. Create(SVarParamNotFound);
  206. VAR_TYPEMISMATCH:
  207. Create(SInvalidVarCast);
  208. VAR_BADVARTYPE:
  209. Create(SVarBadType);
  210. VAR_OVERFLOW:
  211. Create(SVarOverflow);
  212. VAR_BADINDEX:
  213. Create(SVarArrayBounds);
  214. VAR_ARRAYISLOCKED:
  215. Create(SVarArrayLocked);
  216. VAR_NOTIMPL:
  217. Create(SVarNotImplemented);
  218. VAR_OUTOFMEMORY:
  219. Create(SVarOutOfMemory);
  220. VAR_INVALIDARG:
  221. Create(SVarInvalid);
  222. VAR_UNEXPECTED,
  223. VAR_EXCEPTION:
  224. Create(SVarUnexpected);
  225. else
  226. CreateFmt(SUnknownErrorCode,[Code]);
  227. end;
  228. ErrCode:=Code;
  229. end;
  230. {$ifopt S+}
  231. {$define STACKCHECK_WAS_ON}
  232. {$S-}
  233. {$endif OPT S }
  234. Procedure CatchUnhandledException (Obj : TObject; Addr: Pointer; FrameCount: Longint; Frames: PPointer);[public,alias:'FPC_BREAK_UNHANDLED_EXCEPTION'];
  235. Var
  236. Message : String;
  237. i : longint;
  238. hstdout : ^text;
  239. begin
  240. hstdout:=@stdout;
  241. Writeln(hstdout^,'An unhandled exception occurred at $',HexStr(PtrUInt(Addr),sizeof(PtrUInt)*2),' :');
  242. if Obj is exception then
  243. begin
  244. Message:=Exception(Obj).ClassName+' : '+Exception(Obj).Message;
  245. Writeln(hstdout^,Message);
  246. end
  247. else
  248. Writeln(hstdout^,'Exception object ',Obj.ClassName,' is not of class Exception.');
  249. Writeln(hstdout^,BackTraceStrFunc(Addr));
  250. if (FrameCount>0) then
  251. begin
  252. for i:=0 to FrameCount-1 do
  253. Writeln(hstdout^,BackTraceStrFunc(Frames[i]));
  254. end;
  255. Writeln(hstdout^,'');
  256. end;
  257. Var OutOfMemory : EOutOfMemory;
  258. InValidPointer : EInvalidPointer;
  259. Procedure RunErrorToExcept (ErrNo : Longint; Address,Frame : Pointer);
  260. Var E : Exception;
  261. HS : PString;
  262. begin
  263. Case Errno of
  264. 1,203 : E:=OutOfMemory;
  265. 204 : E:=InvalidPointer;
  266. 2,3,4,5,6,100,101,102,103,105,106 : { I/O errors }
  267. begin
  268. Case Errno of
  269. 2 : HS:=@SFileNotFound;
  270. 3 : HS:=@SInvalidFileName;
  271. 4 : HS:=@STooManyOpenFiles;
  272. 5 : HS:=@SAccessDenied;
  273. 6 : HS:=@SInvalidFileHandle;
  274. 15 : HS:=@SInvalidDrive;
  275. 100 : HS:=@SEndOfFile;
  276. 101 : HS:=@SDiskFull;
  277. 102 : HS:=@SFileNotAssigned;
  278. 103 : HS:=@SFileNotOpen;
  279. 104 : HS:=@SFileNotOpenForInput;
  280. 105 : HS:=@SFileNotOpenForOutput;
  281. 106 : HS:=@SInvalidInput;
  282. end;
  283. E:=EinOutError.Create (HS^);
  284. // this routine can be called from FPC_IOCHECK,
  285. // which clears inoutres and then passes its
  286. // original value to HandleErrorFrame() (which calls
  287. // us). So use errno rather than IOResult, and clear
  288. // InOutRes explicitly in case we can also be called
  289. // from a place that does not clear InOutRes explicitly
  290. EInoutError(E).ErrorCode:=errno;
  291. inoutres:=0;
  292. end;
  293. // We don't set abstracterrorhandler, but we do it here.
  294. // Unless the use sets another handler we'll get here anyway...
  295. 200 : E:=EDivByZero.Create(SDivByZero);
  296. 201 : E:=ERangeError.Create(SRangeError);
  297. 205 : E:=EOverflow.Create(SOverflow);
  298. 206 : E:=EOverflow.Create(SUnderflow);
  299. 207 : E:=EInvalidOp.Create(SInvalidOp);
  300. 211 : E:=EAbstractError.Create(SAbstractError);
  301. 212 : E:=EExternalException.Create(SExternalException);
  302. 214 : E:=EBusError.Create(SBusError);
  303. 215 : E:=EIntOverflow.Create(SIntOverflow);
  304. 216 : E:=EAccessViolation.Create(SAccessViolation);
  305. 217 : E:=EControlC.Create(SControlC);
  306. 218 : E:=EPrivilege.Create(SPrivilege);
  307. 219 : E:=EInvalidCast.Create(SInvalidCast);
  308. 220 : E:=EVariantError.Create(SInvalidVarCast);
  309. 221 : E:=EVariantError.Create(SInvalidVarOp);
  310. 222 : E:=EVariantError.Create(SDispatchError);
  311. 223 : E:=EVariantError.Create(SVarArrayCreate);
  312. 224 : E:=EVariantError.Create(SVarNotArray);
  313. 225 : E:=EVariantError.Create(SVarArrayBounds);
  314. 227 : E:=EAssertionFailed.Create(SAssertionFailed);
  315. 228 : E:=EIntfCastError.Create(SIntfCastError);
  316. 229 : E:=ESafecallException.Create(SSafecallException);
  317. 231 : E:=EConvertError.Create(SiconvError);
  318. 232 : E:=ENoThreadSupport.Create(SNoThreadSupport);
  319. 233 : E:=ENoWideStringSupport.Create(SSigQuit);
  320. 234 : E:=ENoWideStringSupport.Create(SMissingWStringManager);
  321. else
  322. E:=Exception.CreateFmt (SUnKnownRunTimeError,[Errno]);
  323. end;
  324. Raise E at Address,Frame;
  325. end;
  326. {$IFDEF HAS_OSERROR}
  327. Procedure RaiseLastOSError;overload;
  328. begin
  329. RaiseLastOSError(GetLastOSError);
  330. end;
  331. Procedure RaiseLastOSError(LastError: Integer);overload;
  332. var
  333. E : EOSError;
  334. begin
  335. If (LastError<>0) then
  336. E:=EOSError.CreateFmt(SOSError, [LastError, SysErrorMessage(LastError)])
  337. else
  338. E:=EOSError.Create(SUnkOSError);
  339. E.ErrorCode:=LastError;
  340. Raise E;
  341. end;
  342. {$else}
  343. Procedure RaiseLastOSError;overload;
  344. begin
  345. Raise Exception.Create('RaiseLastOSError not implemented on this platform.');
  346. end;
  347. Procedure RaiseLastOSError(LastError: Integer);overload;
  348. begin
  349. RaiseLastOSError;
  350. end;
  351. {$endif}
  352. Procedure AssertErrorHandler (Const Msg,FN : ShortString;LineNo:longint; TheAddr : pointer);
  353. Var
  354. S : String;
  355. begin
  356. If Msg='' then
  357. S:=SAssertionFailed
  358. else
  359. S:=Msg;
  360. Raise EAssertionFailed.Createfmt(SAssertError,[S,Fn,LineNo]) at get_caller_addr(theAddr), get_caller_frame(theAddr);
  361. end;
  362. {$ifdef STACKCHECK_WAS_ON}
  363. {$S+}
  364. {$endif}
  365. Procedure InitExceptions;
  366. {
  367. Must install uncaught exception handler (ExceptProc)
  368. and install exceptions for system exceptions or signals.
  369. (e.g: SIGSEGV -> ESegFault or so.)
  370. }
  371. begin
  372. ExceptionClass := Exception;
  373. ExceptProc:=@CatchUnhandledException;
  374. // Create objects that may have problems when there is no memory.
  375. OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
  376. OutOfMemory.AllowFree:=false;
  377. InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
  378. InvalidPointer.AllowFree:=false;
  379. AssertErrorProc:=@AssertErrorHandler;
  380. ErrorProc:=@RunErrorToExcept;
  381. OnShowException:=Nil;
  382. end;
  383. Procedure DoneExceptions;
  384. begin
  385. OutOfMemory.AllowFree:=true;
  386. OutOfMemory.Free;
  387. InValidPointer.AllowFree:=true;
  388. InValidPointer.Free;
  389. end;
  390. { Exception handling routines }
  391. function ExceptObject: TObject;
  392. begin
  393. If RaiseList=Nil then
  394. Result:=Nil
  395. else
  396. Result:=RaiseList^.FObject;
  397. end;
  398. function ExceptAddr: Pointer;
  399. begin
  400. If RaiseList=Nil then
  401. Result:=Nil
  402. else
  403. Result:=RaiseList^.Addr;
  404. end;
  405. function ExceptFrameCount: Longint;
  406. begin
  407. If RaiseList=Nil then
  408. Result:=0
  409. else
  410. Result:=RaiseList^.Framecount;
  411. end;
  412. function ExceptFrames: PPointer;
  413. begin
  414. If RaiseList=Nil then
  415. Result:=Nil
  416. else
  417. Result:=RaiseList^.Frames;
  418. end;
  419. function ExceptionErrorMessage(ExceptObject: TObject; ExceptAddr: Pointer;
  420. Buffer: PChar; Size: Integer): Integer;
  421. Var
  422. S : AnsiString;
  423. Len : Integer;
  424. begin
  425. S:=Format(SExceptionErrorMessage,[ExceptAddr,ExceptObject.ClassName]);
  426. If ExceptObject is Exception then
  427. S:=Format('%s:'#10'%s',[S,Exception(ExceptObject).Message]);
  428. Len:=Length(S);
  429. If S[Len]<>'.' then
  430. begin
  431. S:=S+'.';
  432. Inc(len);
  433. end;
  434. If Len>Size then
  435. Len:=Size;
  436. if Len > 0 then
  437. Move(S[1],Buffer^,Len);
  438. Result:=Len;
  439. end;
  440. procedure ShowException(ExceptObject: TObject; ExceptAddr: Pointer);
  441. // use shortstring. On exception, the heap may be corrupt.
  442. Var
  443. Buf : ShortString;
  444. begin
  445. SetLength(Buf,ExceptionErrorMessage(ExceptObject,ExceptAddr,@Buf[1],255));
  446. If IsConsole Then
  447. writeln(Buf)
  448. else
  449. If Assigned(OnShowException) Then
  450. OnShowException (Buf);
  451. end;
  452. procedure Abort;
  453. begin
  454. Raise EAbort.Create(SAbortError) at Pointer(Get_Caller_addr(Get_Frame));
  455. end;
  456. procedure OutOfMemoryError;
  457. begin
  458. Raise OutOfMemory;
  459. end;
  460. { ---------------------------------------------------------------------
  461. Initialization/Finalization/exit code
  462. ---------------------------------------------------------------------}
  463. Type
  464. PPRecord = ^TPRecord;
  465. TPRecord = Record
  466. Func : TTerminateProc;
  467. NextFunc : PPRecord;
  468. end;
  469. Const
  470. TPList : PPRecord = Nil;
  471. procedure AddTerminateProc(TermProc: TTerminateProc);
  472. Var
  473. TPR : PPRecord;
  474. begin
  475. New(TPR);
  476. With TPR^ do
  477. begin
  478. NextFunc:=TPList;
  479. Func:=TermProc;
  480. end;
  481. TPList:=TPR;
  482. end;
  483. function CallTerminateProcs: Boolean;
  484. Var
  485. TPR : PPRecord;
  486. begin
  487. Result:=True;
  488. TPR:=TPList;
  489. While Result and (TPR<>Nil) do
  490. begin
  491. Result:=TPR^.Func();
  492. TPR:=TPR^.NextFunc;
  493. end;
  494. end;
  495. { ---------------------------------------------------------------------
  496. Diskh functions, OS independent.
  497. ---------------------------------------------------------------------}
  498. function ForceDirectories(Const Dir: string): Boolean;
  499. var
  500. E: EInOutError;
  501. ADrv : String;
  502. function DoForceDirectories(Const Dir: string): Boolean;
  503. var
  504. ADir : String;
  505. APath: String;
  506. begin
  507. Result:=True;
  508. ADir:=ExcludeTrailingPathDelimiter(Dir);
  509. if (ADir='') then Exit;
  510. if Not DirectoryExists(ADir) then
  511. begin
  512. APath := ExtractFilePath(ADir);
  513. //this can happen on Windows if user specifies Dir like \user\name/test/
  514. //and would, if not checked for, cause an infinite recusrsion and a stack overflow
  515. if (APath = ADir) then Result := False
  516. else Result:=DoForceDirectories(APath);
  517. If Result then
  518. Result := CreateDir(ADir);
  519. end;
  520. end;
  521. function IsUncDrive(const Drv: String): Boolean;
  522. begin
  523. Result := (Length(Drv) > 2) and (Drv[1] = PathDelim) and (Drv[2] = PathDelim);
  524. end;
  525. begin
  526. Result := False;
  527. ADrv := ExtractFileDrive(Dir);
  528. if (ADrv<>'') and (not DirectoryExists(ADrv))
  529. {$IFNDEF FORCEDIR_NO_UNC_SUPPORT} and (not IsUncDrive(ADrv)){$ENDIF} then Exit;
  530. if Dir='' then
  531. begin
  532. E:=EInOutError.Create(SCannotCreateEmptyDir);
  533. E.ErrorCode:=3;
  534. Raise E;
  535. end;
  536. Result := DoForceDirectories(SetDirSeparators(Dir));
  537. end;
  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:=GetSSECSR;
  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. SetSSECSR(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,GetModuleFileName(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;