dynlibs.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 dependent part for 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 readinterface}
  12. { ---------------------------------------------------------------------
  13. Interface declarations
  14. ---------------------------------------------------------------------}
  15. type
  16. TLibHandle = longint;
  17. const
  18. NilHandle = 0;
  19. // these are for easier crossplatform construction of dll names in dynloading libs.
  20. SharedSuffix = 'dll';
  21. {$else}
  22. { ---------------------------------------------------------------------
  23. Implementation section
  24. ---------------------------------------------------------------------}
  25. uses
  26. DosCalls;
  27. threadvar
  28. DynLibErrNo: cardinal;
  29. DynLibErrPath: array [0..259] of char;
  30. function LoadLibrary (const Name: AnsiString): TLibHandle;
  31. var
  32. Handle: longint;
  33. begin
  34. DynLibErrPath [0] := #0;
  35. DynLibErrNo := DosLoadModule (@DynLibErrPath [0], SizeOf (DynLibErrPath),
  36. PChar (Name), Handle);
  37. if DynLibErrNo = 0 then
  38. Result := Handle
  39. else
  40. Result := NilHandle;
  41. end;
  42. function GetProcedureAddress (Lib: TLibHandle; const ProcName: AnsiString): pointer;
  43. var
  44. P: pointer;
  45. begin
  46. DynLibErrPath [0] := #0;
  47. DynLibErrNo := DosQueryProcAddr (Lib, 0, PChar (ProcName), P);
  48. if DynLibErrNo = 0 then
  49. Result := P
  50. else
  51. Result := nil;
  52. end;
  53. function UnloadLibrary (Lib: TLibHandle): boolean;
  54. begin
  55. DynLibErrPath [0] := #0;
  56. DynLibErrNo := DosFreeModule (Lib);
  57. Result := DynLibErrNo = 0;
  58. end;
  59. function GetDynLibsError: longint;
  60. begin
  61. GetDynLibsError := DynLibErrNo;
  62. end;
  63. function GetDynLibsErrorStr: string;
  64. const
  65. SysMsgFile: array [0..10] of char = 'OSO001.MSG'#0;
  66. var
  67. VarArr: array [1..9] of PChar;
  68. OutBuf: array [0..999] of char;
  69. RetMsgSize: cardinal;
  70. RC: cardinal;
  71. begin
  72. if DynLibErrNo = 0 then
  73. GetDynLibsErrorStr := ''
  74. else
  75. begin
  76. VarArr [1] := @DynLibErrPath [0];
  77. RC := DosGetMessage (@VarArr, 1, @OutBuf [0], SizeOf (OutBuf),
  78. DynLibErrNo, @SysMsgFile [0], RetMsgSize);
  79. if RC = 0 then
  80. Result := StrPas (@OutBuf [0])
  81. else
  82. begin
  83. WriteStr (Result, DynLibErrNo);
  84. Result := 'Error ' + Result;
  85. end;
  86. if DynLibErrPath [0] <> #0 then
  87. Result := StrPas (@DynLibErrPath [0]) + ' - ' + Result;
  88. end;
  89. end;
  90. function GetLoadErrorStr: string;
  91. begin
  92. GetLoadErrorStr := GetDynLibsErrorStr;
  93. end;
  94. {$endif}