dynlibs.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. {$i rtldefs.inc}
  17. { ---------------------------------------------------------------------
  18. Read OS-dependent interface declarations.
  19. ---------------------------------------------------------------------}
  20. { Note: should define the TOrdinalEntry type and define
  21. DYNLIBS_SUPPORTS_ORDINAL if the operating system supports loading
  22. functions by a ordinal like e.g. Windows or OS/2 do }
  23. {$define readinterface}
  24. {$i dynlibs.inc}
  25. {$undef readinterface}
  26. { ---------------------------------------------------------------------
  27. OS - Independent declarations.
  28. ---------------------------------------------------------------------}
  29. {$IFNDEF DYNLIBS_SUPPORTS_ORDINAL}
  30. type
  31. TOrdinalEntry = SizeUInt;
  32. {$ENDIF DYNLIBS_SUPPORTS_ORDINAL}
  33. Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  34. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  35. Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  36. Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
  37. Function GetProcedureAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  38. Function GetProcedureAddress(Lib : TLibHandle; Ordinal: TOrdinalEntry) : Pointer;
  39. Function UnloadLibrary(Lib : TLibHandle) : Boolean;
  40. Function GetLoadErrorStr: string;
  41. // Kylix/Delphi compability
  42. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  43. Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  44. Type
  45. HModule = TLibHandle;
  46. Implementation
  47. { ---------------------------------------------------------------------
  48. OS - Independent declarations.
  49. ---------------------------------------------------------------------}
  50. { Note: should define the TOrdinalEntry overload if the operating system
  51. supports loading functions by a ordinal like e.g. Windows or OS/2 do }
  52. {$i dynlibs.inc}
  53. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  54. Function DoSafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  55. {$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  56. Function DoSafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  57. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  58. {$if defined(cpui386) or defined(cpux86_64)}
  59. var
  60. fpucw : Word;
  61. ssecw : DWord;
  62. {$endif}
  63. begin
  64. try
  65. {$if defined(cpui386) or defined(cpux86_64)}
  66. fpucw:=Get8087CW;
  67. {$ifdef cpui386}
  68. if has_sse_support then
  69. {$endif cpui386}
  70. ssecw:=GetSSECSR;
  71. {$endif}
  72. Result:=doloadlibrary(Name);
  73. finally
  74. {$if defined(cpui386) or defined(cpux86_64)}
  75. Set8087CW(fpucw);
  76. {$ifdef cpui386}
  77. if has_sse_support then
  78. {$endif cpui386}
  79. SetSSECSR(ssecw);
  80. {$endif}
  81. end;
  82. end;
  83. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  84. Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  85. begin
  86. Result:=DoSafeLoadLibrary(UnicodeString(Name));
  87. end;
  88. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  89. begin
  90. Result:=DoLoadLibrary(UnicodeString(Name));
  91. end;
  92. {$else not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  93. Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  94. begin
  95. Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  96. end;
  97. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  98. begin
  99. Result:=DoLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  100. end;
  101. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  102. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  103. Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  104. begin
  105. Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  106. end;
  107. Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
  108. begin
  109. Result:=DoLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  110. end;
  111. {$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  112. Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  113. begin
  114. Result:=DoSafeLoadLibrary(Name);
  115. end;
  116. Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
  117. begin
  118. Result:=DoLoadLibrary(Name);
  119. end;
  120. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  121. {$ifndef DYNLIBS_SUPPORTS_ORDINAL}
  122. { OS does not support loading by ordinal (or it's not implemented yet), so by
  123. default we simply return Nil }
  124. Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : Pointer;
  125. begin
  126. Result := Nil;
  127. end;
  128. {$endif not DYNLIBS_SUPPORTS_ORDINAL}
  129. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  130. begin
  131. Result:=UnloadLibrary(lib);
  132. end;
  133. Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  134. begin
  135. Result:=GetProcedureAddress(Lib,Procname);
  136. end;
  137. end.