在 Class 中创建回调函数的另一种方法:我将以 SetTimer 为例:首先我们可以在类中声明回调函数:
type
TForm3 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
ListBox2: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
IDTimer, IDTimer2: DWORD;
FObj: Pointer;
procedure FTimerMethod2(var Message: TTimerStruct);
procedure FTimerMethod(_hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;
dwTime: DWORD); stdcall;
public
{ Public declarations }
end;
然后,处理函数:
procedure TForm3.FTimerMethod(_hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;
dwTime: DWORD); stdcall;
begin
{ Note : you can not access function params correctly }
{ & you should use Form3.ListBox2.Items to access the ListBox
instead of ListBox2.Items .
}
{
this Code will not work :
ListBox2.Items.add(IntToStr(_hwnd)); !!!
}
Form3.ListBox2.Items.add(IntToStr(_hwnd));
end;
但请注意,您不能访问函数参数,并且您需要指定保存要与其联系的对象(ListBox2)的全局变量(Form3)。我认为这种方法只适用于 Settimer 回调函数。
其次:使用MakeXObjectInstance从我们的回调函数创建一个对象:这意味着我们的回调函数将被复制到类中..我们首先正常调用回调函数然后我们将所有函数参数转换为TObject类中的函数:添加这个单元第一的 :
unit uTimer;
{uTimer Unit by S.Mahdi}
interface
uses Windows;
type
TTimerStruct = record
_hwnd: HWND;
uMsg: UINT;
idEvent: UINT_PTR;
dwTime: DWORD;
end;
type
TTimerMethod = procedure(var Message: TTimerStruct) of object;
function MakeTimerObjectInstance(const AMethod: TTimerMethod): Pointer;
procedure FreeTimerObjectInstance(ObjectInstance: Pointer);
implementation
type
PObjectInstance = ^TObjectInstance;
TObjectInstance = packed record
Code: Byte;
Offset: Integer;
case Integer of
0:
(Next: PObjectInstance);
1:
(FMethod: TMethod);
end;
const
{$IF Defined(CPUX86)}
CodeBytes = 2;
{$ELSEIF Defined(CPUX64)}
CodeBytes = 8;
{$ENDIF CPU}
InstanceCount = (4096 - SizeOf(Pointer) * 2 - CodeBytes)
div SizeOf(TObjectInstance) - 1;
type
PInstanceBlock = ^TInstanceBlock;
TInstanceBlock = packed record
Next: PInstanceBlock;
Code: array [1 .. CodeBytes] of Byte;
WndProcPtr: Pointer;
Instances: array [0 .. InstanceCount] of TObjectInstance;
end;
var
InstBlockList: PInstanceBlock;
InstFreeList: PObjectInstance;
function CalcJmpOffset(Src, Dest: Pointer): Longint;
begin
Result := IntPtr(Dest) - (IntPtr(Src) + 5);
end;
procedure StdTimerProc(_hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;
dwTime: DWORD); stdcall;
var
TimerStruct: TTimerStruct;
{$IF Defined(CPUX86)}
{ In ECX = Address of method pointer }
asm
PUSH EBX
PUSH EDX
MOV EBX,_hwnd
XOR EDX,EDX
LEA EDX,TimerStruct
MOV [EDX].TTimerStruct._hwnd,EBX;
MOV EBX,uMsg
MOV [EDX].TTimerStruct.uMsg,EBX;
MOV EBX,idEvent
MOV [EDX].TTimerStruct.idEvent,EBX;
MOV EBX,dwTime
MOV [EDX].TTimerStruct.dwTime,EBX;
PUSH EDX
MOV EAX,[ECX].Longint[4]
CALL [ECX].Pointer
POP EDX
POP EBX
(* XOR EAX,EAX
PUSH EAX
PUSH dwTime
PUSH idEvent
PUSH uMsg
PUSH _hwnd
MOV EDX,ESP
MOV EAX,[ECX].Longint[4]
CALL [ECX].Pointer
ADD ESP,16
POP EAX *)
end;
{$ELSEIF Defined(CPUX64)}
{ In R11 = Address of method pointer }
asm
.PARAMS 1
MOV TimerStruct._hwnd,_hwnd;
MOV TimerStruct.uMsg,uMsg;
MOV TimerStruct.idEvent,idEvent;
MOV TimerStruct.dwTime,dwTime;
LEA RDX,TimerStruct
PUSH RCX
PUSH R11
MOV RCX,[R11].TMethod.Data
CALL [R11].TMethod.Code
POP R11
POP RCX
end;
{$ENDIF CPUX64}
function MakeTimerObjectInstance(const AMethod: TTimerMethod): Pointer;
const
BlockCode: array [1 .. CodeBytes] of Byte = (
{$IF Defined(CPUX86)}
$59, { POP ECX }
$E9); { JMP StdTimerProc }
{$ELSEIF Defined(CPUX64)}
$41, $5B, { POP R11 }
$FF, $25, $00, $00, $00, $00)
; { JMP [RIP+0] }
{$ENDIF}
PageSize = 4096;
var
Block: PInstanceBlock;
Instance: PObjectInstance;
begin
if InstFreeList = nil then
begin
Block := VirtualAlloc(nil, PageSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Block^.Next := InstBlockList;
Move(BlockCode, Block^.Code, SizeOf(BlockCode));
{$IF Defined(CPUX86)}
Block^.WndProcPtr := Pointer(CalcJmpOffset(@Block^.Code[2],
@StdTimerProc));
{$ELSEIF Defined(CPUX64)}
Block^.WndProcPtr := @StdTimerProc;
{$ENDIF}
Instance := @Block^.Instances;
repeat
Instance^.Code := $E8; { CALL NEAR PTR Offset }
Instance^.Offset := CalcJmpOffset(Instance, @Block^.Code);
Instance^.Next := InstFreeList;
InstFreeList := Instance;
Inc(PByte(Instance), SizeOf(TObjectInstance));
until IntPtr(Instance) - IntPtr(Block) >= SizeOf(TInstanceBlock);
InstBlockList := Block;
end;
Result := InstFreeList;
Instance := InstFreeList;
InstFreeList := Instance^.Next;
Instance^.FMethod := TMethod(AMethod);
end;
procedure FreeTimerObjectInstance(ObjectInstance: Pointer);
begin
if ObjectInstance <> nil then
begin
PObjectInstance(ObjectInstance)^.Next := InstFreeList;
InstFreeList := ObjectInstance;
end;
end;
end.
这是一个如何使用这两种方法的简单示例:
unit uMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
uTimer;
type
TForm3 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
ListBox2: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
IDTimer, IDTimer2: DWORD;
FObj: Pointer;
procedure FTimerMethod(_hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;
dwTime: DWORD); stdcall;
procedure FTimerMethod2(var Message: TTimerStruct);
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button2Click(Sender: TObject);
begin
KillTimer(Handle, IDTimer);
FreeTimerObjectInstance(FObj);
end;
procedure TForm3.FTimerMethod(_hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;
dwTime: DWORD); stdcall;
begin
{ Note : you can not access function params correctly }
{ & you should use Form3.ListBox2.Items to access the ListBox
instead of ListBox2.Items .
}
{
this Code will not work :
ListBox2.Items.add(IntToStr(_hwnd)); !!!
}
Form3.ListBox2.Items.add(IntToStr(_hwnd));
end;
procedure TForm3.FTimerMethod2(var Message: TTimerStruct);
begin
ListBox1.Items.add(IntToStr(Message._hwnd));
end;
procedure TForm3.Button1Click(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
FObj := MakeTimerObjectInstance(FTimerMethod2);
IDTimer := SetTimer(Handle, 0, 1000, FObj);
IDTimer2 := SetTimer(Handle, 1, 1000, @TForm3.FTimerMethod);
end;
end.