使用ASM调用对象方法 [英] Call Object Method using ASM
问题描述
为了更好地解释什么,我试图完成,我将开始与一些作品。
To better explain what I'm trying to accomplish, I'm going to start with something that works.
假设我们有一个可以调用其他程序和字符串参数传递给它一个过程:
Say we have a procedure that can call another procedure and pass a string parameter to it:
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;
现在我可以叫 SaySomething 像这样(测试和工程(:):
Now I can call SaySomething like so(tested and works (: ):
CallSaySomething(@SaySomething, 'Morning people!');
我的问题是,我怎么能实现类似的功能,但这次的 SaySomething 应该是一个方式
type
TMyObj = class
public
procedure SaySomething(const AValue: string); // calls show message by passing AValue
end;
所以,如果你在我身边还是......,我的目标就是进入类似的过程:
so, if you're still with me..., my goal is to get to a procedure similar to:
procedure CallMyObj(AObjInstance, AObjMethod: Pointer; const AValue: string);
begin
asm
// here is where I need help...
end;
end;
我已经给了它不少镜头,但我的装配知识是有限的。
I've gave it quite a few shots, but my assembly knowledge is limited.
推荐答案
什么是使用ASM的原因是什么?
what is the reason to use asm?
当你调用对象的方法,那么实例指针必须在方法调用中的第一个参数
when you are calling objects method, then instance pointer have to be the first parameter in method call
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.
这篇关于使用ASM调用对象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!