troubleshooting Question

Another SetWindowsHookEx problem. Setting ThreadID to 0 in Delphi 7 but getting an Application Hook instead

Avatar of Kyle Foster
Kyle FosterFlag for United States of America asked on
Delphi
8 Comments1 Solution1527 ViewsLast Modified:
I have a Windows Hook DLL where I am trying to capture LButtonDown throughout the system to close one or more of a PopupMenu that I have created myself.  It is descended from TComponent so it has nothing to do with the Windows common popupmenu control, but it must close itself if the user clicks anywhere else on the screen.

I am only getting the WM_LBUTTONDOWN message when I click on my app so it appears that I am not getting a system wide hook.  It is behaving like an Application Hook.

Can someone look at the syntax because I've went over it 100 times and I am probably missing something stupid.

-----------------------------

here are the HookMouse,UnHookMouse and HookProc functions from the DLL,

you can ignore the parts about the FormDebug (that is working) and the ComponentHandles (there may be more than once instance of the component so I keep a list.  That is also working)

---------------
function HookProc(nCode: Integer; MsgID: WParam; Data: LParam): LResult; stdcall;
var
    ndx     : Integer;
    aHandle : Integer;
begin
    Result := CallNextHookEx(Hook,nCode,MsgID,Data);

    try
        if MsgID=WM_LBUTTONDOWN then begin
            if FormLog<>nil then
                formLog.mmoLOG.Lines.Add('Left Mouse Down Received');

            if ComponentHandles<>nil then
                for ndx := 0 to ComponentHandles.Count-1 do begin
                    aHandle := StrToIntDef(ComponentHandles[ndx],0);
                    if (aHandle<>0) then
                        PostMessage(aHandle, WM_CloseIt, 0, 0);
                end;
        end;
    except
    end;
end;

procedure HookMouse(TheComponentHandle:HWnd;Debug:Boolean); stdcall;
begin
    if ComponentHandles=nil then
        ComponentHandles := TStringlist.Create;

    if (FormLog=nil) and Debug then begin
        FormLog := TFormLog.Create(nil);
        FormLog.Show;
    end;

    ComponentHandles.Add(IntToStr(TheComponentHandle));

    if Hook = 0 then
        Hook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);
end;

procedure UnHookMouse(TheComponentHandle:HWnd); stdcall;
var
    ndx : Integer;
begin
    ndx := ComponentHandles.IndexOf(IntToStr(TheComponentHandle));
    if ndx>=0 then
        ComponentHandles.Delete(ndx);

    if ComponentHandles.count=0 then begin
        ComponentHandles.Free;
        ComponentHandles := nil;

        UnhookWindowsHookEx(Hook);
        Hook:=0;

        FormLog.Close;
        FormLog.Free;
        FormLog := nil;
    end;
end;

ASKER CERTIFIED SOLUTION
Member_2_248744

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 8 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 8 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004