dynlibs.pas 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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(Name : AnsiString) : TLibHandle;
  26. Function LoadLibrary(Name : AnsiString) : TLibHandle;
  27. Function GetProcedureAddress(Lib : TlibHandle; ProcName : AnsiString) : Pointer;
  28. Function UnloadLibrary(Lib : TLibHandle) : Boolean;
  29. // Kylix/Delphi compability
  30. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  31. Function GetProcAddress(Lib : TlibHandle; ProcName : AnsiString) : Pointer;
  32. Type
  33. HModule = TLibHandle;
  34. // these are for easier crossplatform construction of dll names in dynloading libs.
  35. Const
  36. {$ifdef Windows}
  37. SharedSuffix = 'dll';
  38. {$else}
  39. {$ifdef Darwin}
  40. SharedSuffix = 'dylib';
  41. {$else}
  42. {$ifdef OS2}
  43. SharedSuffix = 'dll';
  44. {$else}
  45. SharedSuffix = 'so';
  46. {$endif}
  47. {$endif}
  48. {$endif}
  49. Implementation
  50. { ---------------------------------------------------------------------
  51. OS - Independent declarations.
  52. ---------------------------------------------------------------------}
  53. {$i dynlibs.inc}
  54. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  55. begin
  56. Result:=UnloadLibrary(lib);
  57. end;
  58. Function GetProcAddress(Lib : TlibHandle; ProcName : AnsiString) : Pointer;
  59. begin
  60. Result:=GetProcedureAddress(Lib,Procname);
  61. end;
  62. Function SafeLoadLibrary(Name : AnsiString) : TLibHandle;
  63. {$ifdef i386}
  64. var w : word;
  65. {$endif}
  66. Begin
  67. {$ifdef i386}
  68. w:=get8087cw;
  69. {$endif}
  70. result:=loadlibrary(name);
  71. {$ifdef i386}
  72. set8087cw(w);
  73. {$endif}
  74. End;
  75. end.