4

Using the Microsoft Spy++ I see that Notepad++ receives a WM_SETTEXT message, when you open/create a new document. I need to hook title changes on Windows, so I'm trying doing WH_GETMESSAGE hook, and filtering only WM_SETTEXT. But so far I'm unsuccessful. Here's my DLL:

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

var
  CurrentHook: HHOOK;

{$R *.res}

function GetMessageHookProc(Code: Integer; iWParam: WPARAM; iLParam: LPARAM): LRESULT; stdcall;
begin
  Result:= CallNextHookEx(CurrentHook, Code, iWParam, iLParam);
  if (Code = HC_ACTION) and (PMSG(iLParam).message = wm_settext) then
  begin
    MessageBox(0, 'WM_SETTEXT', 'WM_SETTEXT', MB_OK);

    //this code below is just a prototype to what I will try when this works:
    if IntToStr(PMSG(iLParam).lParam) = 'new - Notepad++' then
      MessageBox(0, 'Notepad++', 'Notepad++', MB_OK);

  end;
end;

procedure SetHook; stdcall;
begin
  CurrentHook:= SetWindowsHookEx(WH_GETMESSAGE, @GetMessageHookProc, HInstance, 0);
  if CurrentHook <> 0 then
    MessageBox(0, 'HOOKED', 'HOOKED', MB_OK);
end;

procedure UnsetHook; stdcall;
begin
  UnhookWindowsHookEx(CurrentHook);
end;

exports
  SetHook,
  UnsetHook;

begin

end.

I get the 'HOOKED' message box, indicating that the hook was settled, but I never get the 'WM_SETTEXT' message box inside the if of the callback procedure. How can I filter only this kind of message, and check the string of the message?

Thank you!

LessStress
  • 187
  • 2
  • 17

1 Answers1

5

WM_SETTEXT is a sent message, not a posted message. A WH_GETMESSAGE hook only sees messages that are posted to the target thread's message queue, so it will never see WM_SETTEXT messages. To hook messages that are sent directly to a window without going through the message queue, you need to use a WH_CALLWNDPROC or WH_CALLWNDPROCRET hook instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Seriously? Thank god I did this question, because I would never figured out what is happening... And strangely, I found some examples on the Internet that shows WM_SETTEXT with WH_GETMESSAGE hook. I don't know why, because analysing your assumptions, none of them makes sense... lol! Should I accept this answer or you want to formulate and example using the CALLWNDPROC hook? – LessStress Feb 18 '16 at 16:49
  • 1
    Spy++ shows you when a logged message is posted vs sent. Pay attention to its output. As for the hook itself, a `WH_CALLWNDPROC/RET` hook is not much different than a `WH_GETMESSAGE` hook in terms of coding. Replace `PMSG` with [`PCWPSTRUCT`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644964.aspx)/[`PCWPRETSTRUCT`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644963.aspx) and use its message-related fields as needed. Read the documentation – Remy Lebeau Feb 18 '16 at 17:01