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

{ 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值