dynlibs.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. {$define readinterface}
  21. {$i dynlibs.inc}
  22. {$undef readinterface}
  23. { ---------------------------------------------------------------------
  24. OS - Independent declarations.
  25. ---------------------------------------------------------------------}
  26. Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  27. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  28. Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  29. Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
  30. Function GetProcedureAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  31. Function UnloadLibrary(Lib : TLibHandle) : Boolean;
  32. Function GetLoadErrorStr: string;
  33. // Kylix/Delphi compability
  34. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  35. Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  36. Type
  37. HModule = TLibHandle;
  38. Implementation
  39. { ---------------------------------------------------------------------
  40. OS - Independent declarations.
  41. ---------------------------------------------------------------------}
  42. {$i dynlibs.inc}
  43. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  44. Function DoSafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  45. {$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  46. Function DoSafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  47. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  48. {$if defined(cpui386) or defined(cpux86_64)}
  49. var
  50. fpucw : Word;
  51. ssecw : DWord;
  52. {$endif}
  53. begin
  54. try
  55. {$if defined(cpui386) or defined(cpux86_64)}
  56. fpucw:=Get8087CW;
  57. {$ifdef cpui386}
  58. if has_sse_support then
  59. {$endif cpui386}
  60. ssecw:=GetSSECSR;
  61. {$endif}
  62. Result:=doloadlibrary(Name);
  63. finally
  64. {$if defined(cpui386) or defined(cpux86_64)}
  65. Set8087CW(fpucw);
  66. {$ifdef cpui386}
  67. if has_sse_support then
  68. {$endif cpui386}
  69. SetSSECSR(ssecw);
  70. {$endif}
  71. end;
  72. end;
  73. {$ifndef FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  74. Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  75. begin
  76. Result:=DoSafeLoadLibrary(UnicodeString(Name));
  77. end;
  78. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  79. begin
  80. Result:=DoLoadLibrary(UnicodeString(Name));
  81. end;
  82. {$else not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  83. Function SafeLoadLibrary(const Name : RawByteString) : TLibHandle;
  84. begin
  85. Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  86. end;
  87. Function LoadLibrary(const Name : RawByteString) : TLibHandle;
  88. begin
  89. Result:=DoLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  90. end;
  91. {$endif not FPCRTL_FILESYSTEM_SINGLE_BYTE_API}
  92. {$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
  93. Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  94. begin
  95. Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  96. end;
  97. Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
  98. begin
  99. Result:=DoLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
  100. end;
  101. {$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  102. Function SafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
  103. begin
  104. Result:=DoSafeLoadLibrary(Name);
  105. end;
  106. Function LoadLibrary(const Name : UnicodeString) : TLibHandle;
  107. begin
  108. Result:=DoLoadLibrary(Name);
  109. end;
  110. {$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
  111. Function FreeLibrary(Lib : TLibHandle) : Boolean;
  112. begin
  113. Result:=UnloadLibrary(lib);
  114. end;
  115. Function GetProcAddress(Lib : TlibHandle; const ProcName : AnsiString) : Pointer;
  116. begin
  117. Result:=GetProcedureAddress(Lib,Procname);
  118. end;
  119. end.