dynlib.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2015 by the Free Pascal development team.
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. { ---------------------------------------------------------------------
  11. OS - Independent declarations.
  12. ---------------------------------------------------------------------}
  13. Var
  14. CurrentDLM : TDynLibsManager;
  15. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  16. Function DoSafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  17. {$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  18. Function DoSafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  19. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  20. {$if defined(cpui386) or defined(cpux86_64)}
  21. var
  22. fpucw : Word;
  23. ssecw : DWord;
  24. {$endif}
  25. begin
  26. try
  27. {$if defined(cpui386) or defined(cpux86_64)}
  28. fpucw:=Get8087CW;
  29. {$ifdef cpui386}
  30. if has_sse_support then
  31. {$endif cpui386}
  32. ssecw:=GetMXCSR;
  33. {$endif}
  34. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  35. Result:=CurrentDLM.LoadLibraryA(Name);
  36. {$else FPCRTL_FILESYSTEM_TWO_BYTE_API}
  37. Result:=CurrentDLM.LoadLibraryU(Name);
  38. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  39. finally
  40. {$if defined(cpui386) or defined(cpux86_64)}
  41. Set8087CW(fpucw);
  42. {$ifdef cpui386}
  43. if has_sse_support then
  44. {$endif cpui386}
  45. SetMXCSR(ssecw);
  46. {$endif}
  47. end;
  48. end;
  49. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  50. begin
  51. Result:=CurrentDLM.LoadLibraryA(Name);
  52. end;
  53. Function LoadLibrary(const Name: UnicodeString) : TLibHandle;
  54. begin
  55. Result:=CurrentDLM.LoadLibraryU(Name);
  56. end;
  57. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  58. Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
  59. begin
  60. Result:=DoSafeLoadLibrary(Name);
  61. end;
  62. Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
  63. begin
  64. Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  65. end;
  66. {$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  67. Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
  68. begin
  69. Result:=DoSafeLoadLibrary(UnicodeString(Name));
  70. end;
  71. Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
  72. begin
  73. Result:=DoSafeLoadLibrary(Name);
  74. end;
  75. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  76. Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
  77. begin
  78. Result:=CurrentDLM.GetProcAddress(Lib, ProcName);
  79. end;
  80. Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
  81. begin
  82. if Assigned(CurrentDLM.GetProcAddressOrdinal) then
  83. Result:=CurrentDLM.GetProcAddressOrdinal(Lib, Ordinal)
  84. else
  85. Result:=Nil;
  86. end;
  87. Function UnloadLibrary(Lib : TLibHandle) : Boolean;
  88. begin
  89. Result:=CurrentDLM.UnloadLibrary(lib);
  90. end;
  91. function GetLoadErrorStr: String;
  92. begin
  93. Result:=CurrentDLM.GetLoadErrorStr();
  94. end;
  95. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  96. begin
  97. Result:=UnloadLibrary(lib);
  98. end;
  99. Function GetProcAddress(Lib : TLibHandle; const ProcName : AnsiString) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
  100. begin
  101. Result:=GetProcedureAddress(Lib,Procname);
  102. end;
  103. Procedure GetDynLibsManager (Var Manager : TDynLibsManager);
  104. begin
  105. Manager:=CurrentDLM;
  106. end;
  107. Procedure SetDynLibsManager (Const New : TDynLibsManager);
  108. begin
  109. CurrentDLM:=New;
  110. end;
  111. Procedure SetDynLibsManager (Const New : TDynLibsManager; Var Old: TDynLibsManager);
  112. begin
  113. Old:=CurrentDLM;
  114. CurrentDLM:=New;
  115. end;
  116. { ---------------------------------------------------------------------
  117. DynLibsManager which gives run-time error. Use if no thread support.
  118. ---------------------------------------------------------------------}
  119. {$ifndef DISABLE_NO_DYNLIBS_MANAGER}
  120. { resourcestrings are not supported by the system unit,
  121. they are in the objpas unit and not available for fpc/tp modes }
  122. const
  123. SNoDynLibs = 'This binary has no dynamic library support compiled in.';
  124. SRecompileWithDynLibs = 'Recompile the application with a dynamic-library-driver in the program uses clause before other units using dynamic libraries.';
  125. Procedure NoDynLibsError; {$ifndef ver2_6}noreturn;{$endif}
  126. begin
  127. {$ifndef EMBEDDED}
  128. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  129. If IsConsole then
  130. begin
  131. Writeln(StdErr,SNoDynLibs);
  132. Writeln(StdErr,SRecompileWithDynLibs);
  133. end;
  134. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  135. {$endif EMBEDDED}
  136. RunError(235)
  137. end;
  138. Function NoLoadLibraryU(const Name: UnicodeString): TLibHandle; {$ifndef ver2_6}noreturn;{$endif}
  139. begin
  140. NoDynLibsError;
  141. end;
  142. Function NoLoadLibraryA(const Name: RawByteString): TLibHandle; {$ifndef ver2_6}noreturn;{$endif}
  143. begin
  144. NoDynLibsError;
  145. end;
  146. function NoGetProcAddress(Lib: TLibHandle; const Proc: AnsiString): {$ifdef cpui8086}FarPointer{$else}Pointer{$endif}; {$ifndef ver2_6}noreturn;{$endif}
  147. begin
  148. NoDynLibsError;
  149. end;
  150. function NoGetProcAddressOrdinal(Lib: TLibHandle; Ordinal: TOrdinalEntry): {$ifdef cpui8086}FarPointer{$else}Pointer{$endif}; {$ifndef ver2_6}noreturn;{$endif}
  151. begin
  152. NoDynLibsError;
  153. end;
  154. function NoGetLoadErrorStr: String; {$ifndef ver2_6}noreturn;{$endif}
  155. begin
  156. NoDynLibsError;
  157. end;
  158. function NoUnloadLibrary(Lib: TLibHandle): Boolean; {$ifndef ver2_6}noreturn;{$endif}
  159. begin
  160. NoDynLibsError;
  161. end;
  162. const
  163. NoDynLibsManager: TDynLibsManager = (
  164. LoadLibraryU: @NoLoadLibraryU;
  165. LoadLibraryA: @NoLoadLibraryA;
  166. GetProcAddress: @NoGetProcAddress;
  167. GetProcAddressOrdinal: @NoGetProcAddressOrdinal;
  168. UnloadLibrary: @NoUnloadLibrary;
  169. GetLoadErrorStr: @NoGetLoadErrorStr;
  170. );
  171. procedure SetNoDynLibsManager;
  172. begin
  173. SetDynLibsManager(NoDynLibsManager);
  174. end;
  175. procedure InitSystemDynLibs;
  176. begin
  177. SetNoDynLibsManager;
  178. end;
  179. {$endif DISABLE_NO_DYNLIBS_MANAGER}