How to capture all messages sent to the window using the hook WH_CBT and WndProc?

I create a logging system inside the dll, the main task of this is to trap some specific window messages of the target application and perform some tasks. But, unfortunately, only one message gets trapped by the WndProc( WM_GETMINMAXINFO) method . What am I missing? Why aren't all Windows messages logged?

This is the minimum sample code to demonstrate the problem.

Dll

library LogDll;

uses
  Winapi.Windows,
  Winapi.Messages,
  System.IOUtils,
  System.SysUtils,
  System.Classes;

{$R *.res}

type
  TLogInspector = class
  private
    WndHandle: THandle;
    ProcAddrInst: Pointer;
    OrgWndProc: Pointer;
  protected
    function CallOrgWndProc(Message: TMessage): LRESULT;
    procedure WndProc(var Message: TMessage); virtual;
  public
    constructor Create(AHandle: THandle); virtual;
  end;

var
  MainHook: HHook;
  Log : TLogInspector;

function TLogInspector.CallOrgWndProc(Message: TMessage): LRESULT;
begin
  Result := CallWindowProc(OrgWndProc, WndHandle, Message.Msg, Message.wParam,  Message.lParam);
end;

constructor TLogInspector.Create(AHandle: THandle);
begin
  OrgWndProc := Pointer(GetWindowLongPtr(AHandle, GWL_WNDPROC));
  ProcAddrInst := MakeObjectInstance(WndProc);
  WndHandle := AHandle;
  SetWindowLongPtr(WndHandle, GWL_WNDPROC, LONG_PTR(ProcAddrInst));
end;

procedure TLogInspector.WndProc(var Message: TMessage);
begin
  //log the current message
  TFile.AppendAllText('C:\Delphi\log.txt', 'WndProc '+IntToStr(Message.Msg)+sLineBreak);
  //call the org WndProc 
  Message.Result := CallOrgWndProc(Message);
end;


function HookCallBack(nCode: Integer;  _WPARAM: WPARAM; _LPARAM: LPARAM): LRESULT;  stdcall;
var
  lpClassName : array [0 .. 256] of Char;
begin
  if nCode = HCBT_CREATEWND then
    begin
      GetClassName(_WPARAM, lpClassName, 256);
      if lpClassName = 'TForm1' then
        Log:=  TLogInspector.Create(_WPARAM);
    end;

  Result := CallNextHookEx(MainHook, nCode, _WPARAM, _LPARAM);
end;


procedure InitLog; stdcall;
begin
  MainHook := SetWindowsHookEx(WH_CBT, @HookCallBack, 0, GetCurrentThreadId);
end;

procedure DoneLog; stdcall;
begin
  UnhookWindowsHookEx(MainHook);
end;

exports
   InitLog, DoneLog;

begin
end.

Application

type
  TForm1 = class(TForm)
    Button1: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  procedure InitLog; stdcall;  external 'LogDll.dll' name 'InitLog';
  procedure DoneLog; stdcall;  external 'LogDll.dll' name 'DoneLog';

implementation

{$R *.dfm}



initialization
 InitLog;
finalization
 DoneLog;
end.
+4
source share
1 answer

. , , VCL . , VCL:

  • VCL InitWndProc 'controls'.
  • VCL CreateWindowHandle, , , CreateWindowEx.
  • dll , , .
  • dll (WM_GETMINMAXINFO).
  • InitWndProc , MainWndProc .
  • dll .

dll SetWindowLongPtr SetWindowLong InitWndProc 'controls.pas', .


WH_CALLWNDPROC hook , , . :.

function HookCallBack(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  lpClassName : array [0 .. 256] of Char;
begin
  Result := CallNextHookEx(MainHook, nCode, wParam, lParam);
  if nCode >= 0 then begin
    GetClassName(PCWPStruct(lParam).hwnd, lpClassName, 256);
    if lpClassName = 'TForm1' then
      TFile.AppendAllText('C:\Delphi\log.txt', 'WndProc ' +
          IntToStr(PCWPStruct(lParam).message) + sLineBreak);
  end;
end;

procedure InitLog; stdcall;
begin
  MainHook := SetWindowsHookEx(WH_CALLWNDPROC, @HookCallBack, 0, GetCurrentThreadId);
end;

, f.i. WM_NCCREATE.

+3

All Articles