Einzelnen Beitrag anzeigen

Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#10

Re: Adresse eine Funktion / Prozedur ermitteln

  Alt 15. Jun 2008, 00:23
Delphi-Quellcode:

type
  // Deine Proceduren
  TProceduren = class
  published
   // Die proceduren
   class procedure ShowString(S: String);
   // Damit werden sie aufgerufen:
   class procedure RunMethod(AMethod: String; Params: array of Const);
  end;

{ TProceduren }

class procedure TProceduren.RunMethod(AMethod: String;
  Params: array of Const);
var proc: Pointer;
    i, off: Integer;
    max: Integer;
begin
  proc := MethodAddress(AMethod);
  max := Length(Params);

  for i:= 2 to max-1 do
  begin
    off := i*8;
    asm
      mov edx,[Params]
      add edx,off

      movzx ecx,[edx+$04]
      mov edx,[edx]

      sub ecx,3
      jnz @NoExt
        movzx ecx, word ptr [edx+$08]
        push ecx
        push [edx+$04]
        push [edx]
        jmp @LoopEnd
      @NoExt:
        push [edx]
      @LoopEnd:
    end;
  end;

  asm
    mov eax, max
    and eax, eax
    jz @Self
    sub eax,1
    jz @Param1
@Param2:
    mov ecx, [Params];
    add ecx, 8

    movzx eax,[ecx+$04]

    mov ecx,[ecx]

    sub eax, 3
    jnz @Param1
      movzx eax, word ptr [ecx+$08]
      push eax
      push [ecx+$04]
      push [ecx]
@Param1:
    mov edx, [Params]

    movzx eax,[edx+$04]

    mov edx, [edx]

    sub eax, 3
    jnz @Self
      movzx eax, word ptr [edx+$08]
      push eax
      push [edx+$04]
      push [edx]
@Self:
    mov eax,Self

    call proc
  end;
end;

class procedure TProceduren.ShowString(S: String);
begin
  ShowMessage(S);
end;

// Anwendung:
procedure TForm1.Button1Click(Sender: TObject);
begin
  TProceduren.RunMethod('ShowString',['Hallo Welt']);
  TProceduren.RunMethod('ShowString',[Edit1.Text]);
end;
Übergeben werden kann prinzipell ein Array of TVarRec, dass du natürlich auch dynamisch erzeugen (vom Benutzer erzeugen/einstellen) lassen kannst.

Gruß
Neutral General
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat