3

为了更好地解释我要完成的工作,我将从可行的东西开始。

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

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;

我已经给了它很多镜头,但我的组装知识是有限的。

4

1 回答 1

4

使用 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.
于 2012-02-26T13:38:42.963 回答