dynlibs.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. Implement OS-dependent part of dynamic library loading.
  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 readinterface}
  12. { ---------------------------------------------------------------------
  13. Interface declarations
  14. ---------------------------------------------------------------------}
  15. Type
  16. { using PtrInt here is compliant with the other platforms }
  17. TLibHandle = PtrInt;
  18. Const
  19. NilHandle = TLibHandle(0);
  20. // these are for easier crossplatform construction of dll names in dynloading libs.
  21. {$if defined(Darwin)}
  22. SharedSuffix = 'dylib';
  23. {$elseif defined(aix)}
  24. SharedSuffix = 'a';
  25. {$else}
  26. SharedSuffix = 'so';
  27. {$endif}
  28. {$else}
  29. { ---------------------------------------------------------------------
  30. Implementation section
  31. ---------------------------------------------------------------------}
  32. uses dl;
  33. Function DoLoadLibrary(const Name : RawByteString) : TLibHandle;
  34. {$ifdef aix}
  35. var
  36. MemberName: RawByteString;
  37. {$endif}
  38. begin
  39. {$ifndef aix}
  40. Result:=TLibHandle(dlopen(PAnsiChar(Name),RTLD_LAZY));
  41. {$else aix}
  42. { in aix, most shared libraries are static libraries (archives) that contain
  43. a single object: shr.o for 32 bit, shr_64.o for 64 bit. You have to specify
  44. this object file explicitly via the RTLD_MEMBER member flag }
  45. {$ifdef cpu64}
  46. MemberName:='(shr_64.o)';
  47. {$else cpu64}
  48. MemberName:='(shr.o)';
  49. {$endif cpu64}
  50. SetCodePage(MemberName,DefaultFileSystemCodePage,false);
  51. MemberName:=Name+MemberName;
  52. Result:=TLibHandle(dlopen(PAnsiChar(MemberName),RTLD_LAZY or RTLD_MEMBER));
  53. {$endif aix}
  54. end;
  55. Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
  56. begin
  57. Result:=dlsym(lib,pchar(ProcName));
  58. end;
  59. Function UnloadLibrary(Lib : TLibHandle) : Boolean;
  60. begin
  61. Result:=dlClose(Lib)=0;
  62. end;
  63. Function GetLoadErrorStr: string;
  64. begin
  65. Result:=dl.dlerror;
  66. end;
  67. {$endif}