Recursive hook ...

Giganews Newsgroups
Subject: Recursive hook ...
Posted by:  Andrew Jameson (softspotsoftware…@SPAMgmail.com)
Date: Mon, 30 Jun 2008

Hi,

I need my application to respond to the SC_SCREENSAVE and SC_MONITORPOWER
events ... it plays music and I'd like it to stop whilst the screensaver is
running and also if the monitor power saving feature kicks in.

It seems that only the topmost window receives the associated
WM_SYSCOMMANDS - so I wrote a DLL hook with the intention of simply of
echoing the command parameters to my application ... but if I pass on the
WM_SYSCOMMAND to my application then my hook intercepts it and it all goes
horribly recursive and causes a system hang !

In the interim, I've introduced a user message and that all works now ...
but I'd like to understand how to reliably forward a command from a hook
such that the hook does not intercept that particular message.  How ?

(I realize that the user might simply turn the monitor off ... but when
Windows initiates a monitor power save, is there any way to test the status
of the monitor ?  I Googled around and found nothing !)

Thanks

Andrew

function HookCBT(code : integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
const
  UM_MONITORPOWER = WM_APP + 1001;
var
  P1HookRec : PAJHookRec;
  h1MemFile : HWND;
begin
  if (code>=0) then begin
      case code of
        HCBT_SYSCOMMAND : if (wParam and $FFF0 = SC_SCREENSAVE) or (wParam
and $FFF0 = SC_MONITORPOWER) then begin
                            if OpenMMF(MapFilename, h1MemFile,
Pointer(P1HookRec)) then begin
                              PostMessage(P1HookRec^.aHwnd, UM_MONITORPOWER,
wParam, lParam);
                              UnmapViewOfFile(P1HookRec);
                              CloseHandle(h1MemFile);
                            end; {if}
                          end; {if}
      end; {case}
  end;
  Result := CallNextHookEx(HookCallback, Code, wParam, lParam);
end; {HookCBT}

Replies