gpt4 book ai didi

delphi - 使用 ASM 调用对象方法

转载 作者:行者123 更新时间:2023-12-03 15:52:57 24 4
gpt4 key购买 nike

为了更好地解释我想要实现的目标,我将从一些可行的事情开始。

假设我们有一个过程可以调用另一个过程并向其传递一个字符串参数:

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(经过测试并且有效(:):

CallSaySomething(@SaySomething, 'Morning people!');

我的问题是,如何实现类似的功能,但这次 SaySomething 应该是一个方法:

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.

关于delphi - 使用 ASM 调用对象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9453238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com