-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuPSR_dll.pas
283 lines (259 loc) · 6.65 KB
/
uPSR_dll.pas
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
unit uPSR_dll;
{$I PascalScript.inc}
interface
uses
uPSRuntime, uPSUtils;
procedure RegisterDLLRuntime(Caller: TPSExec);
procedure RegisterDLLRuntimeEx(Caller: TPSExec; AddDllProcImport: Boolean);
function ProcessDllImport(Caller: TPSExec; P: TPSExternalProcRec): Boolean;
function ProcessDllImportEx(Caller: TPSExec; P: TPSExternalProcRec; ForceDelayLoad: Boolean): Boolean;
function UnloadProc(Caller: TPSExec; p: TPSExternalProcRec; Global, Stack: TPSStack): Boolean;
implementation
uses
{$IFDEF UNIX}
Unix, baseunix, dynlibs, termio, sockets;
{$ELSE}
Windows;
{$ENDIF}
{
p^.Ext1 contains the pointer to the Proc function
p^.ExportDecl:
'dll:'+DllName+#0+FunctionName+#0+chr(Cc)+Chr(DelayLoad)+Chr(AlternateSearchPath)+VarParams
}
type
PLoadedDll = ^TLoadedDll;
TLoadedDll = record
dllnamehash: Longint;
dllname: tbtstring;
dllhandle: THandle;
end;
TMyExec = class(TPSExec);
PInteger = ^Integer;
procedure LAstErrorFree(Sender: TPSExec; P: PInteger);
begin
dispose(p);
end;
procedure DLLSetLastError(Sender: TPSExec; P: Integer);
var
pz: PInteger;
begin
pz := Sender.FindProcResource(@LastErrorFree);
if pz = nil then
begin
new(pz);
Sender.AddResource(@LastErrorFree, PZ);
end;
pz^ := p;
end;
function DLLGetLastError(Sender: TPSExec): Integer;
var
pz: PInteger;
begin
pz := Sender.FindProcResource(@LastErrorFree);
if pz = nil then
result := 0
else
result := pz^;
end;
procedure DllFree(Sender: TPSExec; P: PLoadedDll);
begin
FreeLibrary(p^.dllhandle);
Dispose(p);
end;
function LoadDll(Caller: TPSExec; P: TPSExternalProcRec): Boolean;
var
s, s2, s3: tbtstring;
h, i: Longint;
ph: PLoadedDll;
dllhandle: THandle;
loadwithalteredsearchpath: Boolean;
begin
s := p.Decl;
Delete(s, 1, 4);
s2 := copy(s, 1, pos(tbtchar(#0), s)-1);
delete(s, 1, length(s2)+1);
h := makehash(s2);
s3 := copy(s, 1, pos(tbtchar(#0), s)-1);
delete(s, 1, length(s3)+1);
loadwithalteredsearchpath := bytebool(s[3]);
i := 2147483647; // maxint
dllhandle := 0;
repeat
ph := Caller.FindProcResource2(@dllFree, i);
if (ph = nil) then
begin
if s2 = '' then
begin
// don't pass an empty filename to LoadLibrary, just treat it as uncallable
p.Ext2 := Pointer(1);
Result := False;
exit;
end;
{$IFDEF UNIX}
dllhandle := LoadLibrary(PChar(s2));
{$ELSE}
if loadwithalteredsearchpath then
dllhandle := LoadLibraryExA(PAnsiChar(AnsiString(s2)), 0, LOAD_WITH_ALTERED_SEARCH_PATH)
else
dllhandle := LoadLibraryA(PAnsiChar(AnsiString(s2)));
{$ENDIF}
if dllhandle = 0 then
begin
p.Ext2 := Pointer(1);
Result := False;
exit;
end;
new(ph);
ph^.dllnamehash := h;
ph^.dllname := s2;
ph^.dllhandle := dllhandle;
Caller.AddResource(@DllFree, ph);
end;
if (ph^.dllnamehash = h) and (ph^.dllname = s2) then
begin
dllhandle := ph^.dllhandle;
end;
until dllhandle <> 0;
p.Ext1 := GetProcAddress(dllhandle, pansichar(s3));
if p.Ext1 = nil then
begin
p.Ext2 := Pointer(1);
Result := false;
exit;
end;
Result := True;
end;
function DllProc(Caller: TPSExec; p: TPSExternalProcRec; Global, Stack: TPSStack): Boolean;
var
i: Longint;
MyList: TIfList;
n: PPSVariantIFC;
CurrStack: Cardinal;
cc: TPSCallingConvention;
s: tbtstring;
begin
if p.Ext2 <> nil then // error
begin
Result := false;
exit;
end;
if p.Ext1 = nil then
begin
if not LoadDll(Caller, P) then
begin
Result := false;
exit;
end;
end;
s := p.Decl;
delete(S, 1, pos(tbtchar(#0), s));
delete(S, 1, pos(tbtchar(#0), s));
if length(S) < 2 then
begin
Result := False;
exit;
end;
cc := TPSCallingConvention(s[1]);
delete(s, 1, 3); // cc + delayload + alternatesearchpath (delayload might also be forced!)
CurrStack := Cardinal(Stack.Count) - Cardinal(length(s));
if s[1] = #0 then inc(CurrStack);
MyList := tIfList.Create;
for i := 2 to length(s) do
begin
MyList.Add(nil);
end;
for i := length(s) downto 2 do
begin
MyList[i - 2] := NewPPSVariantIFC(Stack[CurrStack], s[i] <> #0);
inc(CurrStack);
end;
if s[1] <> #0 then
begin
n := NewPPSVariantIFC(Stack[CurrStack], true);
end else n := nil;
try
TMYExec(Caller).InnerfuseCall(nil, p.Ext1, cc, MyList, n);
{$IFNDEF UNIX}
DLLSetLastError(Caller, GetLastError);
{$ENDIF}
finally
DisposePPSvariantIFC(n);
DisposePPSVariantIFCList(MyList);
end;
result := true;
end;
function ProcessDllImport(Caller: TPSExec; P: TPSExternalProcRec): Boolean;
begin
Result := ProcessDllImportEx(Caller, P, False);
end;
function ProcessDllImportEx(Caller: TPSExec; P: TPSExternalProcRec; ForceDelayLoad: Boolean): Boolean;
var
DelayLoad: Boolean;
s: tbtstring;
begin
if not ForceDelayLoad then begin
s := p.Decl;
Delete(s,1,pos(tbtchar(#0), s));
Delete(s,1,pos(tbtchar(#0), s));
DelayLoad := bytebool(s[2]);
end else
DelayLoad := True;
if DelayLoad then begin
p.ProcPtr := DllProc;
Result := True;
end else begin
p.ProcPtr := DllProc;
Result := LoadDll(Caller, p);
end;
end;
function GetLastErrorProc(Caller: TPSExec; p: TPSExternalProcRec; Global, Stack: TPSStack): Boolean;
begin
Stack.SetInt(-1, DLLGetLastError(Caller));
Result := true;
end;
function UnloadProc(Caller: TPSExec; p: TPSExternalProcRec; Global, Stack: TPSStack): Boolean;
var
h, i: Longint;
pv: TPSProcRec;
ph: PLoadedDll;
sname, s: tbtstring;
begin
sname := Stack.GetAnsiString(-1);
for i := Caller.GetProcCount -1 downto 0 do
begin
pv := Caller.GetProcNo(i);
if not (pv is TPSExternalProcRec) then continue;
if @TPSExternalProcRec(pv).ProcPtr <> @DllProc then continue;
s := (TPSExternalProcRec(pv).Decl);
delete(s,1,4);
if copy(s,1,pos(tbtchar(#0),s)-1) = sname then
begin
TPSExternalProcRec(pv).Ext1 := nil;
end;
end;
h := MakeHash(sname);
i := 2147483647; // maxint
repeat
ph := Caller.FindProcResource2(@dllFree, i);
if (ph = nil) then break;
if (ph.dllnamehash = h) and (ph.dllname = sname) then
begin
FreeLibrary(ph^.dllhandle);
Caller.DeleteResource(ph);
dispose(ph);
end;
until false;
result := true;
end;
procedure RegisterDLLRuntime(Caller: TPSExec);
begin
RegisterDLLRuntimeEx(Caller, True);
end;
procedure RegisterDLLRuntimeEx(Caller: TPSExec; AddDllProcImport: Boolean);
begin
if AddDllProcImport then
Caller.AddSpecialProcImport('dll', @ProcessDllImport, nil);
Caller.RegisterFunctionName('UNLOADDLL', UnloadProc, nil, nil);
Caller.RegisterFunctionName('DLLGETLASTERROR', GetLastErrorProc, nil, nil);
end;
end.