dllfuncs.pp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {
  2. **********************************************************************}
  3. {$MODE OBJFPC}
  4. unit DLLFuncs;
  5. interface
  6. uses SysUtils;
  7. function LoadLibrary(Name: PChar): PtrInt;
  8. function GetProcAddress(Lib: PtrInt; ProcName: PChar): Pointer;
  9. function FreeLibrary(Lib: PtrInt): Boolean;
  10. function getlastdlerror: pchar;
  11. implementation
  12. const
  13. RTLD_LAZY = $001;
  14. RTLD_NOW = $002;
  15. RTLD_BINDING_MASK = $003;
  16. {$ifdef Linux}
  17. function dlopen(Name: PChar; Flags: LongInt) : Pointer; cdecl; external 'dl';
  18. function dlsym(Lib: Pointer; Name: PChar) : Pointer; cdecl; external 'dl';
  19. function dlclose(Lib: Pointer): LongInt; cdecl; external 'dl';
  20. function dlerror: pchar; cdecl; external 'dl';
  21. {$else}
  22. function dlopen(Name: PChar; Flags: LongInt) : Pointer; cdecl; external 'c';
  23. function dlsym(Lib: Pointer; Name: PChar) : Pointer; cdecl; external 'c';
  24. function dlclose(Lib: Pointer): LongInt; cdecl; external 'c';
  25. function dlerror: pchar; cdecl; external 'c';
  26. {$endif}
  27. function getlastdlerror: pchar;
  28. begin
  29. getlastdlerror := dlerror;
  30. end;
  31. function LoadLibrary(Name: PChar): PtrInt;
  32. begin
  33. Result := PtrInt(dlopen(Name, RTLD_LAZY));
  34. end;
  35. function GetProcAddress(Lib: PtrInt; ProcName: PChar): Pointer;
  36. begin
  37. Result := dlsym(Pointer(Lib), ProcName);
  38. end;
  39. function FreeLibrary(Lib: PtrInt): Boolean;
  40. begin
  41. if Lib = 0 then
  42. Result := False
  43. else
  44. Result := dlClose(Pointer(Lib)) = 0;
  45. end;
  46. end.