summaryrefslogtreecommitdiff
path: root/rtl/inc/dynlib.inc
blob: e549929764edffc38a16850f7d4fd3d7696c58bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2015 by the Free Pascal development team.

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

{ ---------------------------------------------------------------------
  OS - Independent declarations.
  ---------------------------------------------------------------------}

Var
  CurrentDLM : TDynLibsManager;

{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
Function DoSafeLoadLibrary(const Name : RawByteString) : TLibHandle;
{$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}
Function DoSafeLoadLibrary(const Name : UnicodeString) : TLibHandle;
{$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}
{$if defined(cpui386) or defined(cpux86_64)}
  var
    fpucw : Word;
    ssecw : DWord;
{$endif}
  begin
    try
{$if defined(cpui386) or defined(cpux86_64)}
      fpucw:=Get8087CW;
{$ifdef cpui386}
      if has_sse_support then
{$endif cpui386}
        ssecw:=GetMXCSR;
{$endif}
{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}
      Result:=CurrentDLM.LoadLibraryA(Name);
{$else FPCRTL_FILESYSTEM_TWO_BYTE_API}
      Result:=CurrentDLM.LoadLibraryU(Name);
{$endif FPCRTL_FILESYSTEM_TWO_BYTE_API}
      finally
{$if defined(cpui386) or defined(cpux86_64)}
      Set8087CW(fpucw);
{$ifdef cpui386}
      if has_sse_support then
{$endif cpui386}
        SetMXCSR(ssecw);
{$endif}
    end;
  end;

Function LoadLibrary(const Name : RawByteString) : TLibHandle;
begin
  Result:=CurrentDLM.LoadLibraryA(Name);
end;

Function LoadLibrary(const Name: UnicodeString) : TLibHandle;
begin
  Result:=CurrentDLM.LoadLibraryU(Name);
end;

{$ifndef FPCRTL_FILESYSTEM_TWO_BYTE_API}

Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
begin
  Result:=DoSafeLoadLibrary(Name);
end;

Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
begin
  Result:=DoSafeLoadLibrary(ToSingleByteFileSystemEncodedFileName(Name));
end;

{$else not FPCRTL_FILESYSTEM_TWO_BYTE_API}

Function SafeLoadLibrary(const Name: RawByteString) : TLibHandle;
begin
  Result:=DoSafeLoadLibrary(UnicodeString(Name));
end;

Function SafeLoadLibrary(const Name: UnicodeString) : TLibHandle;
begin
  Result:=DoSafeLoadLibrary(Name);
end;

{$endif not FPCRTL_FILESYSTEM_TWO_BYTE_API}

Function GetProcedureAddress(Lib : TLibHandle; const ProcName : AnsiString) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
begin
  Result:=CurrentDLM.GetProcAddress(Lib, ProcName);
end;


Function GetProcedureAddress(Lib : TLibHandle; Ordinal : TOrdinalEntry) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
begin
  if Assigned(CurrentDLM.GetProcAddressOrdinal) then
    Result:=CurrentDLM.GetProcAddressOrdinal(Lib, Ordinal)
  else
    Result:=Nil;
end;


Function UnloadLibrary(Lib : TLibHandle) : Boolean;
begin
  Result:=CurrentDLM.UnloadLibrary(lib);
end;


function GetLoadErrorStr: String;
begin
  Result:=CurrentDLM.GetLoadErrorStr();
end;


Function FreeLibrary(Lib : TLibHandle) : Boolean;
begin
  Result:=UnloadLibrary(lib);
end;


Function GetProcAddress(Lib : TLibHandle; const ProcName : AnsiString) : {$ifdef cpui8086}FarPointer{$else}Pointer{$endif};
begin
  Result:=GetProcedureAddress(Lib,Procname);
end;

Procedure GetDynLibsManager (Out Manager : TDynLibsManager);
begin
  Manager:=CurrentDLM;
end;

Procedure SetDynLibsManager (Const New : TDynLibsManager);
begin
  CurrentDLM:=New;
end;


Procedure SetDynLibsManager (Const New : TDynLibsManager; Out Old: TDynLibsManager);
begin
  Old:=CurrentDLM;
  CurrentDLM:=New;
end;

{ ---------------------------------------------------------------------
    DynLibsManager which gives run-time error. Use if no thread support.
  ---------------------------------------------------------------------}

{$ifndef DISABLE_NO_DYNLIBS_MANAGER}

{ resourcestrings are not supported by the system unit,
  they are in the objpas unit and not available for fpc/tp modes }
const
  SNoDynLibs = 'This binary has no dynamic library support compiled in.';
  SRecompileWithDynLibs = 'Recompile the application with a dynamic-library-driver in the program uses clause before other units using dynamic libraries.';

Procedure NoDynLibsError; noreturn;
begin
{$ifndef EMBEDDED}
{$ifdef FPC_HAS_FEATURE_CONSOLEIO}
  If IsConsole then
    begin
    Writeln(StdErr,SNoDynLibs);
    Writeln(StdErr,SRecompileWithDynLibs);
    end;
{$endif FPC_HAS_FEATURE_CONSOLEIO}
{$endif EMBEDDED}
  RunError(235)
end;

Function NoLoadLibraryU(const Name: UnicodeString): TLibHandle; noreturn;
begin
  NoDynLibsError;
end;

Function NoLoadLibraryA(const Name: RawByteString): TLibHandle; noreturn;
begin
  NoDynLibsError;
end;

function NoGetProcAddress(Lib: TLibHandle; const Proc: AnsiString): {$ifdef cpui8086}FarPointer{$else}Pointer{$endif}; noreturn;
begin
  NoDynLibsError;
end;

function NoGetProcAddressOrdinal(Lib: TLibHandle; Ordinal: TOrdinalEntry): {$ifdef cpui8086}FarPointer{$else}Pointer{$endif}; noreturn;
begin
  NoDynLibsError;
end;

function NoGetLoadErrorStr: String; noreturn;
begin
  NoDynLibsError;
end;

function NoUnloadLibrary(Lib: TLibHandle): Boolean; noreturn;
begin
  NoDynLibsError;
end;

const
  NoDynLibsManager: TDynLibsManager = (
    LoadLibraryU: @NoLoadLibraryU;
    LoadLibraryA: @NoLoadLibraryA;
    GetProcAddress: @NoGetProcAddress;
    GetProcAddressOrdinal: @NoGetProcAddressOrdinal;
    UnloadLibrary: @NoUnloadLibrary;
    GetLoadErrorStr: @NoGetLoadErrorStr;
  );

procedure SetNoDynLibsManager;
begin
  SetDynLibsManager(NoDynLibsManager);
end;

procedure InitSystemDynLibs;
begin
  SetNoDynLibsManager;
end;

{$endif DISABLE_NO_DYNLIBS_MANAGER}