dynlibs.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. {$DEFINE DYNLIBS_SUPPORTS_ORDINAL}
  16. type
  17. TLibHandle = longint;
  18. TOrdinalEntry = cardinal;
  19. const
  20. NilHandle = 0;
  21. // these are for easier crossplatform construction of dll names in dynloading libs.
  22. SharedSuffix = 'dll';
  23. {$else}
  24. { ---------------------------------------------------------------------
  25. Implementation section
  26. ---------------------------------------------------------------------}
  27. uses
  28. DosCalls;
  29. threadvar
  30. DynLibErrNo: cardinal;
  31. DynLibErrPath: array [0..259] of char;
  32. function DoLoadLibrary (const Name: RawbyteString): TLibHandle;
  33. var
  34. Handle: longint;
  35. begin
  36. DynLibErrPath [0] := #0;
  37. DynLibErrNo := DosLoadModule (@DynLibErrPath [0], SizeOf (DynLibErrPath),
  38. PAnsiChar (Name), Handle);
  39. if DynLibErrNo = 0 then
  40. Result := Handle
  41. else
  42. begin
  43. Result := NilHandle;
  44. OSErrorWatch (DynLibErrNo);
  45. end;
  46. end;
  47. function GetProcedureAddress (Lib: TLibHandle; const ProcName: AnsiString): pointer;
  48. var
  49. P: pointer;
  50. begin
  51. DynLibErrPath [0] := #0;
  52. DynLibErrNo := DosQueryProcAddr (Lib, 0, PChar (ProcName), P);
  53. if DynLibErrNo = 0 then
  54. Result := P
  55. else
  56. begin
  57. Result := nil;
  58. OSErrorWatch (DynLibErrNo);
  59. end;
  60. end;
  61. function GetProcedureAddress (Lib: TLibHandle; Ordinal: TOrdinalEntry): pointer;
  62. var
  63. P: pointer;
  64. begin
  65. DynLibErrPath [0] := #0;
  66. DynLibErrNo := DosQueryProcAddr (Lib, Ordinal, nil, P);
  67. if DynLibErrNo = 0 then
  68. Result := P
  69. else
  70. begin
  71. Result := nil;
  72. OSErrorWatch (DynLibErrNo);
  73. end;
  74. end;
  75. function UnloadLibrary (Lib: TLibHandle): boolean;
  76. begin
  77. DynLibErrPath [0] := #0;
  78. DynLibErrNo := DosFreeModule (Lib);
  79. Result := DynLibErrNo = 0;
  80. if DynLibErrNo <> 0 then
  81. OSErrorWatch (DynLibErrNo);
  82. end;
  83. function GetDynLibsError: longint;
  84. begin
  85. GetDynLibsError := DynLibErrNo;
  86. end;
  87. function GetDynLibsErrorStr: string;
  88. const
  89. SysMsgFile: array [0..10] of char = 'OSO001.MSG'#0;
  90. var
  91. VarArr: array [1..9] of PChar;
  92. OutBuf: array [0..999] of char;
  93. RetMsgSize: cardinal;
  94. RC: cardinal;
  95. begin
  96. if DynLibErrNo = 0 then
  97. GetDynLibsErrorStr := ''
  98. else
  99. begin
  100. Result := '';
  101. VarArr [1] := @DynLibErrPath [0];
  102. RC := DosGetMessage (@VarArr, 1, @OutBuf [0], SizeOf (OutBuf),
  103. DynLibErrNo, @SysMsgFile [0], RetMsgSize);
  104. if RC = 0 then
  105. begin
  106. SetLength (Result, RetMsgSize);
  107. Move (OutBuf [0], Result [1], RetMsgSize);
  108. end
  109. else
  110. begin
  111. Str (DynLibErrNo, Result);
  112. Result := 'Error ' + Result;
  113. if DynLibErrPath [0] <> #0 then
  114. Result := StrPas (@DynLibErrPath [0]) + ' - ' + Result;
  115. OSErrorWatch (RC);
  116. end;
  117. end;
  118. end;
  119. function GetLoadErrorStr: string;
  120. begin
  121. GetLoadErrorStr := GetDynLibsErrorStr;
  122. end;
  123. {$endif}