sysdl.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. threadvar
  12. DynLibErrNo: cardinal;
  13. DynLibErrPath: array [0..259] of char;
  14. function SysLoadLibraryA (const Name: RawbyteString): TLibHandle;
  15. var
  16. Handle: longint;
  17. begin
  18. DynLibErrPath [0] := #0;
  19. DynLibErrNo := DosLoadModule (@DynLibErrPath [0], SizeOf (DynLibErrPath),
  20. PAnsiChar (Name), Handle);
  21. if DynLibErrNo = 0 then
  22. Result := Handle
  23. else
  24. begin
  25. Result := NilHandle;
  26. OSErrorWatch (DynLibErrNo);
  27. end;
  28. end;
  29. function SysLoadLibraryU (const Name: UnicodeString): TLibHandle;
  30. begin
  31. Result := SysLoadLibraryA(ToSingleByteFileSystemEncodedFileName(Name));
  32. end;
  33. function SysGetProcedureAddress (Lib: TLibHandle; const ProcName: AnsiString): pointer;
  34. var
  35. P: pointer;
  36. begin
  37. DynLibErrPath [0] := #0;
  38. DynLibErrNo := DosQueryProcAddr (Lib, 0, PChar (ProcName), P);
  39. if DynLibErrNo = 0 then
  40. Result := P
  41. else
  42. begin
  43. Result := nil;
  44. OSErrorWatch (DynLibErrNo);
  45. end;
  46. end;
  47. function SysGetProcedureAddressOrdinal (Lib: TLibHandle; Ordinal: TOrdinalEntry): pointer;
  48. var
  49. P: pointer;
  50. begin
  51. DynLibErrPath [0] := #0;
  52. DynLibErrNo := DosQueryProcAddr (Lib, Ordinal, nil, 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 SysUnloadLibrary (Lib: TLibHandle): boolean;
  62. begin
  63. DynLibErrPath [0] := #0;
  64. DynLibErrNo := DosFreeModule (Lib);
  65. Result := DynLibErrNo = 0;
  66. if DynLibErrNo <> 0 then
  67. OSErrorWatch (DynLibErrNo);
  68. end;
  69. function GetDynLibsError: longint;
  70. begin
  71. GetDynLibsError := DynLibErrNo;
  72. end;
  73. function GetDynLibsErrPath: PChar;
  74. begin
  75. GetDynLibsErrPath := @DynLibErrPath [0];
  76. end;
  77. function SysGetDynLibsErrorStr: string;
  78. const
  79. SysMsgFile: array [0..10] of char = 'OSO001.MSG'#0;
  80. var
  81. VarArr: array [1..9] of PChar;
  82. OutBuf: array [0..999] of char;
  83. RetMsgSize: cardinal;
  84. RC: cardinal;
  85. I: cardinal;
  86. AErr: ansistring;
  87. begin
  88. if DynLibErrNo = 0 then
  89. SysGetDynLibsErrorStr := ''
  90. else
  91. begin
  92. Result := '';
  93. VarArr [1] := @DynLibErrPath [0];
  94. RC := DosGetMessage (@VarArr, 1, @OutBuf [0], SizeOf (OutBuf),
  95. DynLibErrNo, @SysMsgFile [0], RetMsgSize);
  96. if RC = 0 then
  97. begin
  98. SetLength (Result, RetMsgSize);
  99. Move (OutBuf [0], Result [1], RetMsgSize);
  100. AErr := ansistring (PChar (@DynlibErrPath [0]));
  101. if (AErr <> '') and (Pos (AErr, Result) = 0) then
  102. begin
  103. if Result [Length (Result)] in [#13, #10, ' '] then
  104. Result := Result + '(' + AErr + ')'
  105. else
  106. Result := Result + ' (' + AErr + ')';
  107. end
  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 SysGetLoadErrorStr: string;
  120. begin
  121. SysGetLoadErrorStr := SysGetDynLibsErrorStr;
  122. end;
  123. const
  124. SysDynLibsManager: TDynLibsManager = (
  125. LoadLibraryU: @SysLoadLibraryU;
  126. LoadLibraryA: @SysLoadLibraryA;
  127. GetProcAddress: @SysGetProcedureAddress;
  128. GetProcAddressOrdinal: @SysGetProcedureAddressOrdinal;
  129. UnloadLibrary: @SysUnloadLibrary;
  130. GetLoadErrorStr: @SysGetLoadErrorStr;
  131. );
  132. procedure InitSystemDynLibs;
  133. begin
  134. SetDynLibsManager(SysDynLibsManager);
  135. end;