您的位置:首页 > 编程语言

类成员函数转 Windows 回调函数通用代码单元 - ClassCallback.pas

2016-03-31 15:45 537 查看
{ unit ClassCallback;                                                          }

{                                                                              }

{ A generic solution of make class method to windows callback function         }

{                                                                              }

{ written by savetime, http://savetime.delphibbs.com 2004/6/21                 }

{                                                                              }

{ Usage:                                                                       }

{   1. Include this unit to your delphi project.                               }

{   2. Declare the class callback function same as the corresponding windows   }

{      callback function, notice that must be 'stdcall' function.              }

{   3. Declare an TCallbackInstance field in the class.                        }

{   4. Use MakeCallbackInstance function to make the FCallbackInstance.        }

{   5. Now you can use FCallbackInstance as the windows callback function      }

{                                                                              }

{ Discussion:                                                                  }

{   http://www.delphibbs.com/delphibbs/dispq.asp?lid=2672562                   }

{                                                                              }

unit ClassCallback;

interface

  type TCallbackInstance = array [1..18] of Byte;

  procedure MakeCallbackInstance(var Instance: TCallbackInstance;

    ObjectAddr: Pointer; FunctionAddr: Pointer);

implementation

  {----------------------------}

  { CallbackCode DASM          }

  {----------------------------}

  {    MOV EAX, [ESP];         }

  {    PUSH EAX;               }

  {    MOV EAX, ObjectAddr;    }

  {    MOV [ESP+4], EAX;       }

  {    JMP FunctionAddr;       }

  {----------------------------}

  procedure MakeCallbackInstance(var Instance: TCallbackInstance;

    ObjectAddr: Pointer; FunctionAddr: Pointer);

  const CallbackCode: TCallbackInstance =

    ($8B,$04,$24,$50,$B8,$00,$00,$00,$00,$89,$44,$24,$04,$E9,$00,$00,$00,$00);

  begin

    Move(CallbackCode, Instance, SizeOf(TCallbackInstance));

    PInteger(@Instance[6])^ := Integer(ObjectAddr);

    PInteger(@Instance[15])^ := Integer(Integer(FunctionAddr) - Integer(@Instance) - 18);

  end;

 

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息