dynlibs.pas 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. Implements OS-independent loading of dynamic libraries.
  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. {$IFDEF FPC}
  12. {$MODE OBJFPC}
  13. {$ENDIF}
  14. unit dynlibs;
  15. interface
  16. { ---------------------------------------------------------------------
  17. Read OS-dependent interface declarations.
  18. ---------------------------------------------------------------------}
  19. {$define readinterface}
  20. {$i dynlibs.inc}
  21. {$undef readinterface}
  22. { ---------------------------------------------------------------------
  23. OS - Independent declarations.
  24. ---------------------------------------------------------------------}
  25. Function SafeLoadLibrary(const Name : AnsiString) : TLibHandle;
  26. Function LoadLibrary(const Name : AnsiString) : TLibHandle;
  27. Function GetProcedureAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  28. Function UnloadLibrary(Lib : TLibHandle) : Boolean;
  29. Function GetLoadErrorStr: string;
  30. // Kylix/Delphi compability
  31. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  32. Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  33. Type
  34. HModule = TLibHandle;
  35. Implementation
  36. { ---------------------------------------------------------------------
  37. OS - Independent declarations.
  38. ---------------------------------------------------------------------}
  39. {$i dynlibs.inc}
  40. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  41. begin
  42. Result:=UnloadLibrary(lib);
  43. end;
  44. Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  45. begin
  46. Result:=GetProcedureAddress(Lib,Procname);
  47. end;
  48. Function SafeLoadLibrary(const Name : AnsiString) : TLibHandle;
  49. {$if defined(cpui386) or defined(cpux86_64)}
  50. var
  51. fpucw : Word;
  52. ssecw : DWord;
  53. {$endif}
  54. begin
  55. try
  56. {$if defined(cpui386) or defined(cpux86_64)}
  57. fpucw:=Get8087CW;
  58. {$ifdef cpui386}
  59. if has_sse_support then
  60. {$endif cpui386}
  61. ssecw:=GetSSECSR;
  62. {$endif}
  63. {$if defined(windows) or defined(win32)}
  64. Result:=LoadLibraryA(PChar(Name));
  65. {$else}
  66. Result:=loadlibrary(Name);
  67. {$endif}
  68. finally
  69. {$if defined(cpui386) or defined(cpux86_64)}
  70. Set8087CW(fpucw);
  71. {$ifdef cpui386}
  72. if has_sse_support then
  73. {$endif cpui386}
  74. SetSSECSR(ssecw);
  75. {$endif}
  76. end;
  77. end;
  78. end.