| 1 |
unit ExtDll; |
| 2 |
{$IFDEF FPC} |
| 3 |
{$MODE DELPHI} {$H+} |
| 4 |
{$ENDIF} |
| 5 |
interface |
| 6 |
uses struct; |
| 7 |
|
| 8 |
//procedure PrepareCallback(Proc:TRoutine); |
| 9 |
|
| 10 |
implementation |
| 11 |
uses Windows,SysUtils, Forms,Controls, |
| 12 |
variabl,express,texthand,base,base0,base2,HelpCtex,supplied,SConsts, |
| 13 |
TEXTFRM,PAINTFRM,TRACEFRM,INPUTDLG,CHARINP,LocateFrm; |
| 14 |
|
| 15 |
const DLL_Error=-9900; |
| 16 |
const MissingCALLBACK=-9901; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
{*******} |
| 21 |
{外部DLL} |
| 22 |
{*******} |
| 23 |
type |
| 24 |
PPointerArray=^TPointerArray; |
| 25 |
TPointerArray=array[0..7] of pointer; |
| 26 |
TLongIntFunction=function:LongInt; |
| 27 |
TFPUFunction=function:extended; |
| 28 |
TAssign=class(TStatement) |
| 29 |
Handle:THandle; |
| 30 |
ProcAddr:TLongIntFunction; |
| 31 |
ProcAddrX:TFPUFunction; |
| 32 |
NumParam:integer; |
| 33 |
params:PPointerArray; |
| 34 |
ResultType:char; |
| 35 |
{for gen. code} |
| 36 |
Name1,name2:ansistring; |
| 37 |
constructor create(prev,eld:TStatement); |
| 38 |
//procedure exec;override; |
| 39 |
destructor destroy;override; |
| 40 |
function Code:ansistring;override; |
| 41 |
end; |
| 42 |
|
| 43 |
constructor TAssign.Create(prev,eld:TStatement); |
| 44 |
var |
| 45 |
Routine:TRoutine; |
| 46 |
begin |
| 47 |
inherited create(prev,eld); |
| 48 |
{for gen.code} name1:=Tokenstring; |
| 49 |
routine:=localroutine; |
| 50 |
if routine=nil then routine:=programunit; |
| 51 |
Handle:=LoadLibrary(PChar(ExtractFilePath(Application.ExeName)+TokenString)); |
| 52 |
if Handle=0 then |
| 53 |
Handle:=LoadLibrary(PChar(TokenString)); |
| 54 |
if (Handle=0) then |
| 55 |
SetErr(tokenString + s_cannotbeloaded ,IDH_DLL); |
| 56 |
if Handle<>0 then |
| 57 |
begin |
| 58 |
gettoken; |
| 59 |
check(',',IDH_DLL); |
| 60 |
{for gen.code} name2:=Tokenstring; |
| 61 |
@ProcAddr:=GetProcAddress(Handle,PChar(TokenString)); |
| 62 |
gettoken; |
| 63 |
end ; |
| 64 |
if @ProcAddr=nil then |
| 65 |
SetErr(tokenString+ s_isnotfound,IDH_DLL); |
| 66 |
NumParam:=Routine.paramcount; |
| 67 |
params:=AllocMem(sizeof(pointer)*NumParam); |
| 68 |
|
| 69 |
if (Routine.Resultvar<>nil) and test(',') then |
| 70 |
begin |
| 71 |
CheckToken('FPU',IDH_EXTENSION_MS); |
| 72 |
@ProcAddrX:=@ProcAddr; |
| 73 |
@ProcAddr:=nil; |
| 74 |
end; |
| 75 |
|
| 76 |
end; |
| 77 |
|
| 78 |
destructor TAssign.destroy; |
| 79 |
begin |
| 80 |
FreeLibrary(Handle); |
| 81 |
if params<>nil then freemem(params,sizeof(pointer)*NumParam); |
| 82 |
inherited destroy |
| 83 |
end; |
| 84 |
|
| 85 |
Function GetString(p:PChar):string; |
| 86 |
begin |
| 87 |
result:=p |
| 88 |
end; |
| 89 |
|
| 90 |
Function RoundToLongint(x:extended):longint;assembler; |
| 91 |
asm |
| 92 |
PUSH EDX |
| 93 |
PUSH EAX |
| 94 |
FLD x |
| 95 |
FISTP QWORD PTR [ESP] |
| 96 |
WAIT |
| 97 |
POP EAX |
| 98 |
POP EDX |
| 99 |
end; |
| 100 |
(* |
| 101 |
procedure TAssign.exec; |
| 102 |
var |
| 103 |
i,j:integer; |
| 104 |
p:pointer; |
| 105 |
x:double; |
| 106 |
begin |
| 107 |
i:=0; |
| 108 |
|
| 109 |
try |
| 110 |
while i<NumParam do |
| 111 |
begin |
| 112 |
with TIdRec(Proc.VarTable.items[i]) do |
| 113 |
if kindchar='s' then |
| 114 |
string(params^[i]):=subs.evalS |
| 115 |
else |
| 116 |
begin |
| 117 |
longint(params^[i]):=RoundToLongint(subs.evalX); |
| 118 |
end; |
| 119 |
inc(i); |
| 120 |
end; |
| 121 |
|
| 122 |
j:=i; |
| 123 |
try |
| 124 |
while j>0 do |
| 125 |
begin |
| 126 |
dec(j); |
| 127 |
p:=@params^[j]; |
| 128 |
asm |
| 129 |
mov eax, p |
| 130 |
push dword ptr [eax] |
| 131 |
end; |
| 132 |
end; |
| 133 |
if Proc.resultVar<>nil then |
| 134 |
if @ProcAddr<>nil then |
| 135 |
if Proc.resultVar.kindchar='n' then |
| 136 |
Proc.resultVar.subs.assignLongint(ProcAddr) |
| 137 |
else |
| 138 |
Proc.resultVar.subs.substS(GetString(Pchar(ProcAddr))) |
| 139 |
else |
| 140 |
Proc.resultVar.subs.assignX(ProcAddrX) |
| 141 |
else |
| 142 |
ProcAddr; |
| 143 |
except |
| 144 |
on E:EExtype do |
| 145 |
raise; |
| 146 |
else |
| 147 |
SetException(DLL_Error) |
| 148 |
end; |
| 149 |
|
| 150 |
finally |
| 151 |
while i>0 do |
| 152 |
begin |
| 153 |
dec(i); |
| 154 |
with TIdRec(Proc.VarTable.items[i]) do |
| 155 |
if kindchar='s' then |
| 156 |
string(params^[i]):='' |
| 157 |
else |
| 158 |
longint(params^[i]):=0; |
| 159 |
end; |
| 160 |
end; |
| 161 |
|
| 162 |
end; |
| 163 |
*) |
| 164 |
function TAssign.Code:ansistring; |
| 165 |
var |
| 166 |
i:integer; |
| 167 |
begin |
| 168 |
if @ProcAddrX=nil then |
| 169 |
result:='winlib.ASSIGN(' |
| 170 |
else |
| 171 |
result:='ASSIGNFPU('; |
| 172 |
result:=result+''''+name1+''','''+name2+''','+'['; |
| 173 |
for i:=0 to NumParam-1 do |
| 174 |
begin |
| 175 |
if i>0 then result:=result+','; |
| 176 |
result:=result+ TIdRec(Proc.VarTable.items[i]).Literal; |
| 177 |
end; |
| 178 |
result:=result+'])' ; |
| 179 |
|
| 180 |
if Proc.resultVar<>nil then |
| 181 |
if @ProcAddr<>nil then |
| 182 |
if Proc.resultVar.kindchar='n' then |
| 183 |
result:='result:='+result +';' |
| 184 |
else |
| 185 |
result:='result:=PChar(pointer('+result+'));' |
| 186 |
else |
| 187 |
result:='result:='+result +';' |
| 188 |
else |
| 189 |
result:=result+';'; |
| 190 |
|
| 191 |
end; |
| 192 |
|
| 193 |
function ASSIGNst(prev,eld:TStatement):TStatement; |
| 194 |
begin |
| 195 |
ASSIGNst:=TAssign.create(prev,eld); |
| 196 |
end; |
| 197 |
|
| 198 |
{*************} |
| 199 |
{ CallBack } |
| 200 |
{*************} |
| 201 |
|
| 202 |
type |
| 203 |
TIntArray=array[0..255] of integer; |
| 204 |
PIntArray=^TIntArray; |
| 205 |
|
| 206 |
var |
| 207 |
ProcPtr: array[0..9]of TRoutine; |
| 208 |
NumParams:array[0..9]of integer; |
| 209 |
(* |
| 210 |
function ManageCallBack(n:integer; p:PIntArray):integer; |
| 211 |
var |
| 212 |
i,j:integer; |
| 213 |
svCurrentStatement,svNextStatement:TStatement; |
| 214 |
begin |
| 215 |
result:=0; |
| 216 |
|
| 217 |
with ProcPtr[n] do |
| 218 |
begin |
| 219 |
NumParams[n]:=Paramcount; |
| 220 |
|
| 221 |
VarTable.pushStack; |
| 222 |
for j:=0 to VarTable.count-1 do |
| 223 |
TIdrec(VarTable.items[j]).subs.getVar1; |
| 224 |
|
| 225 |
for i:=0 to ParamCount-1 do |
| 226 |
begin |
| 227 |
with TIdRec(VarTable.items[i]) do |
| 228 |
case kindchar of |
| 229 |
's': |
| 230 |
subs.substS(PChar(p^[i])); |
| 231 |
'n': |
| 232 |
subs.assignLongint(p^[i]); |
| 233 |
end; |
| 234 |
end; |
| 235 |
|
| 236 |
svCurrentStatement:=CurrentStatement; |
| 237 |
svNextStatement:=NextStatement; |
| 238 |
try |
| 239 |
try |
| 240 |
runBlock(block); |
| 241 |
except |
| 242 |
On E:EControlException do |
| 243 |
if (kind='F') and (E is EExitFunction) |
| 244 |
or (kind='S') and (E is EExitSub) |
| 245 |
or (kind='P') and (E is EExitPicture) |
| 246 |
then |
| 247 |
else |
| 248 |
raise |
| 249 |
end; |
| 250 |
if resultvar<>nil then |
| 251 |
with ResultVar do |
| 252 |
case kindchar of |
| 253 |
's': result:=LongInt(Pchar(resultvar.subs.evalS)); |
| 254 |
'n': result:=resultvar.subs.evalLongInt; |
| 255 |
end; |
| 256 |
finally |
| 257 |
CurrentStatement:=svCurrentStatement; |
| 258 |
NextStatement:=svNextStatement; |
| 259 |
|
| 260 |
for j:=VarTable.count -1 downto 0 do |
| 261 |
TIdrec(VarTable.items[j]).FreeVar; |
| 262 |
VarTable.popStack; |
| 263 |
|
| 264 |
propagateException; |
| 265 |
end; |
| 266 |
end; |
| 267 |
end; |
| 268 |
|
| 269 |
function CallBack0(p0:longint):longint; stdcall; |
| 270 |
begin |
| 271 |
result:=ManageCallBack(0,@p0); |
| 272 |
asm |
| 273 |
mov ecx,Dword Ptr Numparams |
| 274 |
dec ecx |
| 275 |
shl ecx,2 //ecxを4倍する |
| 276 |
pop ebp |
| 277 |
pop edx //return address |
| 278 |
add esp,ecx |
| 279 |
push edx |
| 280 |
push ebp |
| 281 |
end; |
| 282 |
end; |
| 283 |
|
| 284 |
function CallBack1(p0:longint):longint; stdcall; |
| 285 |
begin |
| 286 |
result:=ManageCallBack(1,@p0); |
| 287 |
asm |
| 288 |
mov ecx,Dword Ptr Numparams + 4 |
| 289 |
dec ecx |
| 290 |
shl ecx,2 //ecxを4倍する |
| 291 |
pop ebp |
| 292 |
pop edx //return address |
| 293 |
add esp,ecx |
| 294 |
push edx |
| 295 |
push ebp |
| 296 |
end; |
| 297 |
end; |
| 298 |
|
| 299 |
function CallBack2(p0:longint):longint; stdcall; |
| 300 |
begin |
| 301 |
result:=ManageCallBack(2,@p0); |
| 302 |
asm |
| 303 |
mov ecx,Dword Ptr Numparams + 8 |
| 304 |
dec ecx |
| 305 |
shl ecx,2 //ecxを4倍する |
| 306 |
pop ebp |
| 307 |
pop edx //return address |
| 308 |
add esp,ecx |
| 309 |
push edx |
| 310 |
push ebp |
| 311 |
end; |
| 312 |
end; |
| 313 |
|
| 314 |
function CallBack3(p0:longint):longint; stdcall; |
| 315 |
begin |
| 316 |
result:=ManageCallBack(3,@p0); |
| 317 |
asm |
| 318 |
mov ecx,Dword Ptr Numparams + 12 |
| 319 |
dec ecx |
| 320 |
shl ecx,2 //ecxを4倍する |
| 321 |
pop ebp |
| 322 |
pop edx //return address |
| 323 |
add esp,ecx |
| 324 |
push edx |
| 325 |
push ebp |
| 326 |
end; |
| 327 |
end; |
| 328 |
|
| 329 |
function CallBack4(p0:longint):longint; stdcall; |
| 330 |
begin |
| 331 |
result:=ManageCallBack(4,@p0); |
| 332 |
asm |
| 333 |
mov ecx,Dword Ptr Numparams + 16 |
| 334 |
dec ecx |
| 335 |
shl ecx,2 //ecxを4倍する |
| 336 |
pop ebp |
| 337 |
pop edx //return address |
| 338 |
add esp,ecx |
| 339 |
push edx |
| 340 |
push ebp |
| 341 |
end; |
| 342 |
end; |
| 343 |
|
| 344 |
function CallBack5(p0:longint):longint; stdcall; |
| 345 |
begin |
| 346 |
result:=ManageCallBack(5,@p0); |
| 347 |
asm |
| 348 |
mov ecx,Dword Ptr Numparams + 20 |
| 349 |
dec ecx |
| 350 |
shl ecx,2 //ecxを4倍する |
| 351 |
pop ebp |
| 352 |
pop edx //return address |
| 353 |
add esp,ecx |
| 354 |
push edx |
| 355 |
push ebp |
| 356 |
end; |
| 357 |
end; |
| 358 |
|
| 359 |
function CallBack6(p0:longint):longint; stdcall; |
| 360 |
begin |
| 361 |
result:=ManageCallBack(6,@p0); |
| 362 |
asm |
| 363 |
mov ecx,Dword Ptr Numparams + 24 |
| 364 |
dec ecx |
| 365 |
shl ecx,2 //ecxを4倍する |
| 366 |
pop ebp |
| 367 |
pop edx //return address |
| 368 |
add esp,ecx |
| 369 |
push edx |
| 370 |
push ebp |
| 371 |
end; |
| 372 |
end; |
| 373 |
|
| 374 |
function CallBack7(p0:longint):longint; stdcall; |
| 375 |
begin |
| 376 |
result:=ManageCallBack(7,@p0); |
| 377 |
asm |
| 378 |
mov ecx,Dword Ptr Numparams + 28 |
| 379 |
dec ecx |
| 380 |
shl ecx,2 //ecxを4倍する |
| 381 |
pop ebp |
| 382 |
pop edx //return address |
| 383 |
add esp,ecx |
| 384 |
push edx |
| 385 |
push ebp |
| 386 |
end; |
| 387 |
end; |
| 388 |
|
| 389 |
function CallBack8(p0:longint):longint; stdcall; |
| 390 |
begin |
| 391 |
result:=ManageCallBack(8,@p0); |
| 392 |
asm |
| 393 |
mov ecx,Dword Ptr Numparams + 32 |
| 394 |
dec ecx |
| 395 |
shl ecx,2 //ecxを4倍する |
| 396 |
pop ebp |
| 397 |
pop edx //return address |
| 398 |
add esp,ecx |
| 399 |
push edx |
| 400 |
push ebp |
| 401 |
end; |
| 402 |
end; |
| 403 |
|
| 404 |
function CallBack9(p0:longint):longint; stdcall; |
| 405 |
begin |
| 406 |
result:=ManageCallBack(9,@p0); |
| 407 |
asm |
| 408 |
mov ecx,Dword Ptr Numparams + 36 |
| 409 |
dec ecx |
| 410 |
shl ecx,2 //ecxを4倍する |
| 411 |
pop ebp |
| 412 |
pop edx //return address |
| 413 |
add esp,ecx |
| 414 |
push edx |
| 415 |
push ebp |
| 416 |
end; |
| 417 |
end; |
| 418 |
*) |
| 419 |
(* |
| 420 |
type |
| 421 |
TCallBackAdr=class(TMiscInt) |
| 422 |
exp:TPrincipal; |
| 423 |
PUnit:TProgramUnit; |
| 424 |
constructor create; |
| 425 |
function evalLongint:longint;override; |
| 426 |
destructor destroy;override; |
| 427 |
private |
| 428 |
function getAddress(i:integer):integer; |
| 429 |
end; |
| 430 |
|
| 431 |
function TCallBackAdr.getAddress(i:integer):integer; |
| 432 |
begin |
| 433 |
case i of |
| 434 |
0: result:=integer(@CallBack0); |
| 435 |
1: result:=integer(@CallBack1); |
| 436 |
2: result:=integer(@CallBack2); |
| 437 |
3: result:=integer(@CallBack3); |
| 438 |
4: result:=integer(@CallBack4); |
| 439 |
5: result:=integer(@CallBack5); |
| 440 |
6: result:=integer(@CallBack6); |
| 441 |
7: result:=integer(@CallBack7); |
| 442 |
8: result:=integer(@CallBack8); |
| 443 |
9: result:=integer(@CallBack9); |
| 444 |
else result:=0; |
| 445 |
end; |
| 446 |
end; |
| 447 |
|
| 448 |
constructor TCallBackAdr.create; |
| 449 |
var |
| 450 |
i:integer; |
| 451 |
begin |
| 452 |
inherited create; |
| 453 |
check('(',IDH_CALLBACK); |
| 454 |
exp:=NExpression ; |
| 455 |
check(')',IDH_CALLBACK); |
| 456 |
PUnit:=ProgramUnit; |
| 457 |
end; |
| 458 |
|
| 459 |
destructor TCallBackAdr.destroy; |
| 460 |
begin |
| 461 |
inherited destroy; |
| 462 |
end; |
| 463 |
|
| 464 |
function TCallBackAdr.evalLongint:longint; |
| 465 |
var |
| 466 |
i:integer; |
| 467 |
begin |
| 468 |
i:=exp.evalLongint ; |
| 469 |
if (i>=0) and (i<=9) |
| 470 |
and (ProcPtr[i]<>nil) |
| 471 |
and ( not (ProcPtr[i] is TLocalProc) |
| 472 |
or (TLocalProc(ProcPtr[i]).parent=Punit)) then |
| 473 |
result:=GetAddress(i) |
| 474 |
else |
| 475 |
setexception(MissingCallBack); |
| 476 |
|
| 477 |
end; |
| 478 |
|
| 479 |
function CallBackAdrfnc:TPrincipal;far; |
| 480 |
begin |
| 481 |
CallBackAdrFnc:=NOperation(TCallBackAdr.create) |
| 482 |
end; |
| 483 |
|
| 484 |
|
| 485 |
procedure PrepareCallback(Proc:TRoutine); |
| 486 |
var |
| 487 |
i:integer; |
| 488 |
begin |
| 489 |
if (Proc.paramcount=0) then |
| 490 |
seterr('Unsuitable for CallBack',IDH_CALLBACK); |
| 491 |
|
| 492 |
if (length(token)=1) and (token[1]>='0') and (token[1]<='9') then |
| 493 |
begin |
| 494 |
i:=ord(token[1])-ord('0'); |
| 495 |
if (Pass=1) and (ProcPtr[i]<>nil) then |
| 496 |
seterr('Duplicate index',IDH_CALLBACK); |
| 497 |
ProcPtr[i]:=proc; |
| 498 |
gettoken; |
| 499 |
end |
| 500 |
end; |
| 501 |
|
| 502 |
procedure InitCallBack; |
| 503 |
var |
| 504 |
i:integer; |
| 505 |
begin |
| 506 |
for i:=0 to 9 do |
| 507 |
ProcPtr[i]:=nil; |
| 508 |
end; |
| 509 |
*) |
| 510 |
|
| 511 |
{**************} |
| 512 |
{Windows Handle} |
| 513 |
{**************} |
| 514 |
|
| 515 |
type |
| 516 |
TWinHandle=class(TMiscInt) |
| 517 |
exp:TPrincipal; |
| 518 |
constructor create; |
| 519 |
//function evalLongint:longint;override; |
| 520 |
destructor destroy;override; |
| 521 |
function Code:Ansistring;override; |
| 522 |
end; |
| 523 |
|
| 524 |
constructor TWinHandle.create; |
| 525 |
begin |
| 526 |
inherited create; |
| 527 |
check('(',IDH_STRING_FUNCTIONS); |
| 528 |
exp:=SExpression; |
| 529 |
check(')',IDH_STRING_FUNCTIONS); |
| 530 |
end; |
| 531 |
|
| 532 |
destructor TWinHandle.destroy; |
| 533 |
begin |
| 534 |
exp.free; |
| 535 |
inherited destroy; |
| 536 |
end; |
| 537 |
|
| 538 |
function TWinhandle.code:ansistring; |
| 539 |
begin |
| 540 |
result:='WinHandle('+exp.code+')' |
| 541 |
end; |
| 542 |
|
| 543 |
(* |
| 544 |
function TWinHandle.evalLongint:longint; |
| 545 |
var |
| 546 |
s:string; |
| 547 |
w:TWinControl; |
| 548 |
begin |
| 549 |
s:=uppercase(exp.evalS); |
| 550 |
|
| 551 |
if s='MAIN' then |
| 552 |
w:=Application.mainForm |
| 553 |
else if s='TEXT' then |
| 554 |
w:=TextForm |
| 555 |
else if s='GRAPHICS' then |
| 556 |
w:=PaintForm |
| 557 |
else {if s='TRACE' then |
| 558 |
w:=TraceForm |
| 559 |
else} if s='INPUT' then |
| 560 |
w:=InputDialog |
| 561 |
else if s='CHARACTER INPUT' then |
| 562 |
w:=CharInput |
| 563 |
else if s='LOCATE' then |
| 564 |
w:=LocateForm |
| 565 |
else{ if s='TEXTWINDOW1' then |
| 566 |
w:=TextWindow1 |
| 567 |
else if s='TEXTWINDOW2' then |
| 568 |
w:=TextWindow2 |
| 569 |
else if s='RICHEDIT' then |
| 570 |
w:=TextForm.memo1 |
| 571 |
else if s='RICHEDIT1' then |
| 572 |
w:=TextWindow1.memo1 |
| 573 |
else if s='RICHEDIT2' then |
| 574 |
w:=TextWindow2.memo1 |
| 575 |
else} |
| 576 |
w:=nil; |
| 577 |
if w<>nil then |
| 578 |
result:=w.Handle |
| 579 |
else |
| 580 |
result:=0 |
| 581 |
end; |
| 582 |
*) |
| 583 |
|
| 584 |
function WinHandlefnc:TPrincipal;far; |
| 585 |
begin |
| 586 |
WinHandlefnc:=NOperation(TWinHandle.create) |
| 587 |
end; |
| 588 |
|
| 589 |
|
| 590 |
{*************} |
| 591 |
{Win32 API関数} |
| 592 |
{*************} |
| 593 |
|
| 594 |
|
| 595 |
{************} |
| 596 |
{Registration} |
| 597 |
{************} |
| 598 |
|
| 599 |
|
| 600 |
procedure statementTableinit;far; |
| 601 |
begin |
| 602 |
StatementTableInitDeclative ('ASSIGN',ASSIGNst); |
| 603 |
SuppliedFunctionTable.accept('WINHANDLE',WinHandlefnc); |
| 604 |
//SuppliedFunctionTable.accept('CALLBACKADR',CallBackAdrfnc); //未実装 |
| 605 |
//InitCallBack; |
| 606 |
end; |
| 607 |
|
| 608 |
|
| 609 |
begin |
| 610 |
tableInitProcs.accept(statementTableinit); |
| 611 |
end. |
| 612 |
|