dynlib.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. {$ifndef FPUNONE}
  21. var
  22. fpucw: TNativeFPUControlWord;
  23. begin
  24. try
  25. fpucw:=GetNativeFPUControlWord;
  26. {$else}
  27. begin
  28. {$endif}
  29. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  30. Result:=CurrentDLM.LoadLibraryA(Name);
  31. {$else FPCRTL_FILESYSTEM_TWO_BYTE_API}
  32. Result:=CurrentDLM.LoadLibraryU(Name);
  33. {$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
  34. {$ifndef FPUNONE}
  35. finally
  36. SetNativeFPUControlWord(fpucw);
  37. end;
  38. {$endif}
  39. end;
  40. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  41. begin
  42. Result:=CurrentDLM.LoadLibraryA(Name);
  43. end;
  44. Function LoadLibrary(const Name: UnicodeString) : TLibHandle;
  45. begin
  46. Result:=CurrentDLM.LoadLibraryU(Name);
  47. end;
  48. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  49. Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
  50. begin
  51. Result:=DoSafeLoadLibrary(Name);
  52. end;
  53. Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
  54. begin
  55. Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  56. end;
  57. {$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  58. Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
  59. begin
  60. Result:=DoSafeLoadLibrary(UnicodeString(Name));
  61. end;
  62. Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
  63. begin
  64. Result:=DoSafeLoadLibrary(Name);
  65. end;
  66. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  67. Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
  68. begin
  69. Result:=CurrentDLM.GetProcAddress(Lib, ProcName);
  70. end;
  71. Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
  72. begin
  73. if Assigned(CurrentDLM.GetProcAddressOrdinal) then
  74. Result:=CurrentDLM.GetProcAddressOrdinal(Lib, Ordinal)
  75. else
  76. Result:=Nil;
  77. end;
  78. Function UnloadLibrary(Lib : TLibHandle) : Boolean;
  79. begin
  80. Result:=CurrentDLM.UnloadLibrary(lib);
  81. end;
  82. function GetLoadErrorStr: AnsiString;
  83. begin
  84. Result:=CurrentDLM.GetLoadErrorStr();
  85. end;
  86. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  87. begin
  88. Result:=UnloadLibrary(lib);
  89. end;
  90. Function GetProcAddress(Lib : TLibHandle; const ProcName : AnsiString) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
  91. begin
  92. Result:=GetProcedureAddress(Lib,Procname);
  93. end;
  94. Procedure GetDynLibsManager (Out Manager : TDynLibsManager);
  95. begin
  96. Manager:=CurrentDLM;
  97. end;
  98. Procedure SetDynLibsManager (Const New : TDynLibsManager);
  99. begin
  100. CurrentDLM:=New;
  101. end;
  102. Procedure SetDynLibsManager (Const New : TDynLibsManager; Out Old: TDynLibsManager);
  103. begin
  104. Old:=CurrentDLM;
  105. CurrentDLM:=New;
  106. end;
  107. { ---------------------------------------------------------------------
  108. DynLibsManager which gives run-time error. Use if no thread support.
  109. ---------------------------------------------------------------------}
  110. {$ifndef DISABLE_NO_DYNLIBS_MANAGER}
  111. { resourcestrings are not supported by the system unit,
  112. they are in the objpas unit and not available for fpc/tp modes }
  113. const
  114. SNoDynLibs = 'This binary has no dynamic library support compiled in.';
  115. SRecompileWithDynLibs = 'Recompile the application with a dynamic-library-driver in the program uses clause before other units using dynamic libraries.';
  116. Procedure NoDynLibsError; noreturn;
  117. begin
  118. {$ifndef EMBEDDED}
  119. {$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  120. If IsConsole then
  121. begin
  122. Writeln(StdErr,SNoDynLibs);
  123. Writeln(StdErr,SRecompileWithDynLibs);
  124. end;
  125. {$endif FPC_HAS_FEATURE_CONSOLEIO}
  126. {$endif EMBEDDED}
  127. RunError(235)
  128. end;
  129. Function NoLoadLibraryU(const Name: UnicodeString): TLibHandle; noreturn;
  130. begin
  131. NoDynLibsError;
  132. end;
  133. Function NoLoadLibraryA(const Name: RawByteString): TLibHandle; noreturn;
  134. begin
  135. NoDynLibsError;
  136. end;
  137. function NoGetProcAddress(Lib: TLibHandle; const Proc: AnsiString): {$ifdef cpui8086}FarPointer{$else}Pointer{$endif}; noreturn;
  138. begin
  139. NoDynLibsError;
  140. end;
  141. function NoGetProcAddressOrdinal(Lib: TLibHandle; Ordinal: TOrdinalEntry): {$ifdef cpui8086}FarPointer{$else}Pointer{$endif}; noreturn;
  142. begin
  143. NoDynLibsError;
  144. end;
  145. function NoGetLoadErrorStr: AnsiString; noreturn;
  146. begin
  147. NoDynLibsError;
  148. end;
  149. function NoUnloadLibrary(Lib: TLibHandle): Boolean; noreturn;
  150. begin
  151. NoDynLibsError;
  152. end;
  153. const
  154. NoDynLibsManager: TDynLibsManager = (
  155. LoadLibraryU: @NoLoadLibraryU;
  156. LoadLibraryA: @NoLoadLibraryA;
  157. GetProcAddress: @NoGetProcAddress;
  158. GetProcAddressOrdinal: @NoGetProcAddressOrdinal;
  159. UnloadLibrary: @NoUnloadLibrary;
  160. GetLoadErrorStr: @NoGetLoadErrorStr;
  161. );
  162. procedure SetNoDynLibsManager;
  163. begin
  164. SetDynLibsManager(NoDynLibsManager);
  165. end;
  166. procedure InitSystemDynLibs;
  167. begin
  168. SetNoDynLibsManager;
  169. end;
  170. {$endif DISABLE_NO_DYNLIBS_MANAGER}
  171. procedure EnumModules(Func: TEnumModuleFuncLW; Data: Pointer);
  172. begin
  173. Func(HInstance,Data);
  174. end;