Callback a class

This article demonstrates how to use an instance of a class as a callback.

If you wanted a windows callback to call an instance of a class, simply replace TMyClass with your classname, and TMyClass.MyCallBack with your method name.



type

  TCallbackThunk = packed record

    POPEDX: Byte;

    MOVEAX: Byte;

    SelfPtr: Pointer;

    PUSHEAX: Byte;

    PUSHEDX: Byte;

    JMP: Byte;

    JmpOffset: Integer;

  end;

var

  Callback: TCallbackThunk;

begin

  inherited Create;

  Callback.POPEDX := $5A;

  Callback.MOVEAX := $B8;

  Callback.SelfPtr := Self;

  Callback.PUSHEAX := $50;

  Callback.PUSHEDX := $52;



  Callback.JMP := $E9;

  Callback.JmpOffset := Integer(@TMyClass.MyCallBack) - Integer(@Callback.JMP) - 5;



  //Call the WINAPI here, passing @CallBack at the callback proc, for example

  EnumSystemLocales(TFNLocaleEnumProc(@Callback), LCID_SUPPORTED);

end;

 

Share this article!

Follow us!

Find more helpful articles: