Pregunta

Para explicar mejor lo que estoy tratando de lograr, voy a empezar con algo que funcione.

Digamos que tenemos un procedimiento que puede llamar a otro procedimiento y pasar un parámetro de cadena:

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

Este es el procedimiento que llamaremos:

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

Ahora puedo llamar stingomething como SO (probado y funciona (:):

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

Mi pregunta es, ¿cómo puedo lograr una funcionalidad similar, pero esta vez Singtomehing debería ser un método :

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

Entonces, si todavía estás conmigo ..., mi objetivo es llegar a un procedimiento similar a:

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

Lo he dado bastante fotos, pero mi conocimiento de la asamblea es limitado.

¿Fue útil?

Solución

¿Cuál es la razón para usar ASM?

Cuando está llamando a los objetos, el puntero de instancia tiene que ser el primer parámetro en el método llame

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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top