MahdiSafsafi / DDetours

Delphi Detours Library
Mozilla Public License 2.0
373 stars 157 forks source link

How to hook message method? #53

Closed changguangyu closed 6 years ago

changguangyu commented 6 years ago

I want to hook WMActivate.

MahdiSafsafi commented 6 years ago

I already answered this question on G+. However I'm going to re-post the example.

uses DDetours;

type
  TTrampolineWMActivate = procedure(Control: TControl; var Message: TWMActivate);
  TCustomFormHack       = type TCustomForm;

var
  TrampolineWMActivate: TTrampolineWMActivate = nil;

procedure WMActivateHook(Control: TControl; var Message: TWMActivate);
const
  Bool2Str: array [Boolean] of string = ('False', 'True');
begin
  Main.Caption := Format('WMActivate(%s).Active=%s', [Control.Name, Bool2Str[Message.Active <> WA_INACTIVE]]);
  TrampolineWMActivate(Control, Message);
end;

procedure TMain.FormCreate(Sender: TObject);
begin
  @TrampolineWMActivate := InterceptCreate(@TCustomFormHack.WMActivate, @WMActivateHook);
end;
changguangyu commented 6 years ago

Thanks. It works. But why TTrampolineWMActivate = procedure(const Self; var Message: TWMActivate); doesn't work?

MahdiSafsafi commented 6 years ago

That's because Self operand will be passed by ref (normally it defaults to by val).