Pergunta

Para explicar melhor o que estou tentando realizar, começarei com algo que funciona.

Digamos que temos um procedimento que pode chamar outro procedimento e passar um parâmetro de string para ele:

procedure CallSaySomething(AProc: Pointer; const AValue: string);
var
  LAddr: Integer;
begin
  LAddr := Integer(PChar(AValue));
  asm
    MOV EAX, LAddr
    CALL AProc;
  end;
end;

Este é o procedimento que chamaremos de:

procedure SaySomething(const AValue: string);
begin
  ShowMessage( AValue );
end;

Agora eu posso ligar Dizer algo assim(testado e funciona (:):

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

Minha pergunta é: como posso obter funcionalidade semelhante, mas desta vez Dizer algo devia ser um método:

type
  TMyObj = class
  public
    procedure SaySomething(const AValue: string); // calls show message by passing AValue
  end;

então, se você ainda está comigo..., meu objetivo é chegar a um procedimento semelhante a:

procedure CallMyObj(AObjInstance, AObjMethod: Pointer; const AValue: string);
begin
  asm
    // here is where I need help...
  end;
end;

Já tentei algumas vezes, mas meu conhecimento de montagem é limitado.

Foi útil?

Solução

qual é a razão para usar asm?

quando você está chamando o método de objetos, o ponteiro da instância deve ser o primeiro parâmetro na chamada do método

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.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top