使用ASM调用对象方法
-
13-11-2019 - |
题
更好地解释我想要完成的事情,我将从一些有效的东西开始。
假设我们有一个可以调用另一个过程的过程并将字符串参数传递给它:
procedure CallSaySomething(AProc: Pointer; const AValue: string);
var
LAddr: Integer;
begin
LAddr := Integer(PChar(AValue));
asm
MOV EAX, LAddr
CALL AProc;
end;
end;
.
这是我们调用的程序:
procedure SaySomething(const AValue: string);
begin
ShowMessage( AValue );
end;
.
现在我可以称之为 tapeomething (测试和工作(:):
.
CallSaySomething(@SaySomething, 'Morning people!');
我的问题是,我如何实现类似的功能,但这一次 tapeomething 应该是方法:
.
type
TMyObj = class
public
procedure SaySomething(const AValue: string); // calls show message by passing AValue
end;
所以,如果你还和我在一起......,我的目标是进入类似于:的过程
.
procedure CallMyObj(AObjInstance, AObjMethod: Pointer; const AValue: string);
begin
asm
// here is where I need help...
end;
end;
我已经给了它相当多的镜头,但我的装配知识有限。
解决方案
使用ASM的原因是什么?
当您调用对象方法时,实例指针必须是方法调用中的第一个参数
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses System.SysUtils;
type
TTest = class
procedure test(x : integer);
end;
procedure TTest.test(x: integer);
begin
writeln(x);
end;
procedure CallObjMethod(data, code : pointer; value : integer);
begin
asm
mov eax, data;
mov edx, value;
call code;
end;
end;
var t : TTest;
begin
t := TTest.Create();
try
CallObjMethod(t, @TTest.test, 2);
except
end;
readln;
end.
. 不隶属于 StackOverflow