Window hooks problem

I've installed windows hook function using following procedure:

1. Definition of the hook function as a global one:
long __stdcall HookProc( int nCode, WPARAM wCurThread, LPARAM lMessage )
{
 UINT      uiMessage;
 
 if ( nCode == HC_ACTION )
   {
     uiMessage =      ((PCWPSTRUCT) lMessage )->message;
     
     switch ( uiMessage )
     {
       case WM_LBUTTONDBLCLK:                  AfxMessageBox( "DBLCLK!" );
      break;
     }
  } // if...

 // Call next hook proc
 return CallNextHookEx( hHook, nCode, wCurThread, lMessage );
}

2. In OnCreate() handler of the CView window of the SDI:

hHook =      SetWindowsHookEx( WH_CALLWNDPROC, HookProc, NULL,      AfxGetApp()->m_nThreadID);

3. In the OnDestroy():
   
   UnhookWindowsHookEx( hHook );

But messagebox never shows up!
Anyone has idea?
Yhanks!

Apolo.
LVL 5
ekcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ekcAuthor Commented:
Adjusted points to 150
0
jkrCommented:
I've never used a CALLWNDPROC hook, but you might want to consider a WH_GETMESSAGE hook:

LRESULT CALLBACK HookProc   (   int     nCode,  // hook code
                                WPARAM  wParam, // removal flag
                                LPARAM  lParam  // address of structure with message
                            )
{
    PMSG    pmsg    =   ( PMSG) lParam;

    if  (   0   >   nCode   ||  PM_NOREMOVE ==  wParam)
        {
            return  (   CallNextHookEx  (   g_hhk,
                                            nCode,
                                            wParam,
                                            lParam
                                        )
                    );
        }

    if  (  WM_LBUTTONDBLCLK    ==  pmsg->message
        )
        {
            // this one is for us...
        }

    return  (   CallNextHookEx  (   g_hhk,
                                    nCode,
                                    wParam,
                                    lParam
                                )
            );
}

and set it like

g_hhk = SetWindowsHookEx ( WH_GETMESSAGE,
( HOOKPROC) HookProc,
g_hThisDll,
0
);

(this example was a *global* hook, if you want this feature, you'll have to place it in a DLL)
0
ekcAuthor Commented:
I've tried WH_GETMESSAGE also, in the same configuration and it doesn't work.
In fact, I want to develop ActiveX control which will be able to "catch" messages.
Do you think it could work like that?
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

jkrCommented:
When you're using a global hook, it should work...
0
ekcAuthor Commented:
It works!
Thanks!
jkr, Could you lock question, so I can grade you?
0
jkrCommented:
No problem ;-)

Fine that it works!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ekcAuthor Commented:
I've just noticed a big problem, with this!
It cathes events, but never uninstalles!!
I builted ActiveX in whose InitInstance() I call SetWIndowsHookEx(), and in ExitInstance() - UnhookWindowsHookEx()!
When I compliled it (even before embeding in container (!?)), ExitInstance() was invoked and UnhookWindowsHookEx()executed with return value 1!
Later, after any dobleclick in the system, hook procedure was invoked again!
Do you have any idea, what is going on in my poor system? :)

Apolo.
0
jkrCommented:
That sounds strange... Where do you store the hook handle? I usually store it in a shared data section in the DLL and call a function inside the DLL that calls 'UnhookWindowsHookEx()' with the stored handle...
0
ekcAuthor Commented:
I have a class CHook, which incapsulates install/deinstall mechanism, and instance of it is member of the CCMytrlApp class.
Hook handle is global variable in DLL (ActiveX).
I call UnhookWindowsHookEx(), from ExitInstance() of the CCMytrlApp class (as I've told before) through member function of the CHook class...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.