dynlibs.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. Result := NilHandle;
  43. end;
  44. function GetProcedureAddress (Lib: TLibHandle; const ProcName: AnsiString): pointer;
  45. var
  46. P: pointer;
  47. begin
  48. DynLibErrPath [0] := #0;
  49. DynLibErrNo := DosQueryProcAddr (Lib, 0, PChar (ProcName), P);
  50. if DynLibErrNo = 0 then
  51. Result := P
  52. else
  53. Result := nil;
  54. end;
  55. function GetProcedureAddress (Lib: TLibHandle; Ordinal: TOrdinalEntry): pointer;
  56. var
  57. P: pointer;
  58. begin
  59. DynLibErrPath [0] := #0;
  60. DynLibErrNo := DosQueryProcAddr (Lib, Ordinal, nil, P);
  61. if DynLibErrNo = 0 then
  62. Result := P
  63. else
  64. Result := nil;
  65. end;
  66. function UnloadLibrary (Lib: TLibHandle): boolean;
  67. begin
  68. DynLibErrPath [0] := #0;
  69. DynLibErrNo := DosFreeModule (Lib);
  70. Result := DynLibErrNo = 0;
  71. end;
  72. function GetDynLibsError: longint;
  73. begin
  74. GetDynLibsError := DynLibErrNo;
  75. end;
  76. function GetDynLibsErrorStr: string;
  77. const
  78. SysMsgFile: array [0..10] of char = 'OSO001.MSG'#0;
  79. var
  80. VarArr: array [1..9] of PChar;
  81. OutBuf: array [0..999] of char;
  82. RetMsgSize: cardinal;
  83. RC: cardinal;
  84. begin
  85. if DynLibErrNo = 0 then
  86. GetDynLibsErrorStr := ''
  87. else
  88. begin
  89. VarArr [1] := @DynLibErrPath [0];
  90. RC := DosGetMessage (@VarArr, 1, @OutBuf [0], SizeOf (OutBuf),
  91. DynLibErrNo, @SysMsgFile [0], RetMsgSize);
  92. if RC = 0 then
  93. Result := StrPas (@OutBuf [0])
  94. else
  95. begin
  96. Str (DynLibErrNo, Result);
  97. Result := 'Error ' + Result;
  98. end;
  99. if DynLibErrPath [0] <> #0 then
  100. Result := StrPas (@DynLibErrPath [0]) + ' - ' + Result;
  101. end;
  102. end;
  103. function GetLoadErrorStr: string;
  104. begin
  105. GetLoadErrorStr := GetDynLibsErrorStr;
  106. end;
  107. {$endif}