2
0

dynlibs.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 FPC_DOTTEDUNITS}
  12. uses UnixApi.Dl;
  13. {$ELSE}
  14. uses dl;
  15. {$ENDIF}
  16. Function SysLoadLibraryA(const Name : RawByteString) : TLibHandle;
  17. {$ifdef aix}
  18. var
  19. MemberName: RawByteString;
  20. {$endif}
  21. begin
  22. {$ifndef aix}
  23. Result:=TLibHandle(dlopen(PAnsiChar(Name),RTLD_LAZY));
  24. {$else aix}
  25. { in aix, most shared libraries are static libraries (archives) that contain
  26. a single object: shr.o for 32 bit, shr_64.o for 64 bit. You have to specify
  27. this object file explicitly via the RTLD_MEMBER member flag }
  28. {$ifdef cpu64}
  29. MemberName:='(shr_64.o)';
  30. {$else cpu64}
  31. MemberName:='(shr.o)';
  32. {$endif cpu64}
  33. SetCodePage(MemberName,DefaultFileSystemCodePage,false);
  34. MemberName:=Name+MemberName;
  35. Result:=TLibHandle(dlopen(PAnsiChar(MemberName),RTLD_LAZY or RTLD_MEMBER));
  36. {$endif aix}
  37. end;
  38. Function SysLoadLibraryU(const Name: UnicodeString) : TLibHandle;
  39. begin
  40. Result := SysLoadLibraryA(ToSingleByteFileSystemEncodedFileName(Name));
  41. end;
  42. Function SysGetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : Pointer;
  43. begin
  44. Result:=dlsym(lib,PAnsiChar(ProcName));
  45. end;
  46. Function SysUnloadLibrary(Lib : TLibHandle) : Boolean;
  47. begin
  48. Result:=dlClose(Lib)=0;
  49. end;
  50. Function SysGetLoadErrorStr: ansistring;
  51. begin
  52. {$IFDEF FPC_DOTTEDUNITS}
  53. Result:=UnixApi.Dl.dlerror;
  54. {$ELSE}
  55. Result:=dl.dlerror;
  56. {$ENDIF}
  57. end;
  58. const
  59. SysDynLibsManager: TDynLibsManager = (
  60. LoadLibraryU: @SysLoadLibraryU;
  61. LoadLibraryA: @SysLoadLibraryA;
  62. GetProcAddress: @SysGetProcedureAddress;
  63. GetProcAddressOrdinal: Nil;
  64. UnloadLibrary: @SysUnloadLibrary;
  65. GetLoadErrorStr: @SysGetLoadErrorStr;
  66. );
  67. procedure InitDynLibs;
  68. begin
  69. SetDynLibsManager(SysDynLibsManager);
  70. end;