Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetWindowsHookEx lost when switching appl 2

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
i've made a test with SetWindowsHookEx to capture keyboard messages to MY OWN APPLICATION. it works, except the fact that the hook doesn't hook anything else when i use the keyboard in another application. when i switch back to my own application, the hook doesn't hook my keyboard inputs anymore.

why ? how to solve the problem ?

here is how i create the hook :

HHOOK hookdata;
hookdata = SetWindowsHookEx( WH_KEYBOARD, KeyboardProc, hInstance, 0 );

and here is my callback function :

LRESULT CALLBACK KeyboardProc ( int code, WPARAM w, LPARAM l )
{
MessageBox( g_hWnd, "KEYBOARD", "HOOKED", MB_OK );
return 0;
}
 

"An error may occur if the hMod parameter is NULL and the dwThreadId parameter is zero or specifies the identifier of a thread created by another process."

-- What you probably want to do is create a global hook with a dll --:

A good resource would be:
 
luc2,

the mistake u have made is that you have not set GLOBAL hook! ur app only hooks for ur app.

hook has to come from dll.

To set system wide hooks, you need to reference a DLL in the SetWindowsHookEx()-command.

here are some excellent examples with source code:

1. 2. 3.
/Srikanth

Ne'er take a wife till thou hast a house (and a fire) to put her in.
-Benjamin Franklin (1706-1790)
 
just what i was looking for... * for u

If somethings hard to do, its not worth doing - Homer Simpson
 
I have a keyboard hook that is functionning correctly but I am unable to use it as a global hook. It was functionning correctly before but I have made some modification witch should not affect the thread it monitor but now it only work when the controling application(in MFC) as the focus. The hook is contained in a dll.

--Here is how I use it--
The code in the calling C++ MFC app:
Code:
void CButtonWindow::BtnStartClick()
{
  if (kbHookFlag == false)
  {
    if ((hinstDLL = AfxLoadLibrary((LPCTSTR) "hodll.dll")) == NULL)
    {
      // Failed to load the localized resources.
    }
    else
    {
      KBHOOKFCT InstHook=reinterpret_cast<KBHOOKFCT>(GetProcAddress(hinstDLL, "installhook"));
    }
  }
}[\code]

The code in the dll:
[code]BOOL __declspec(dllexport)__stdcall installhook()
{
  hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,NULL);
  return TRUE;
}

LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(
                            int nCode, 
                           WPARAM wParam, 
                            LPARAM lParam)
{
  ...
  LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );	
  return  RetVal;
}

BOOL __declspec(dllexport)__stdcall unHook()
{
  BOOL unhooked = UnhookWindowsHookEx(hkb);
  hkb=NULL;
  return unhooked;
} [\code]

I really don't know what to do next to make it act as a global hook. Anyone could help me? Feel free to ask questions, I will do my best to answer them.
 
I've been messing with it, and I may have found the solution to your problem. I tried just storing the HWND in a global variable in the DLL, and then using PostMessage with that. That works as long as the window is focused, but for some reason that I don't understand, it ceases to work correctly once the window loses focus. If you do this instead, it works correctly when the window does not have focus (at least for me):

HWND hwnd = FindWindow("#32770", "My Window Title");
PostMessage(hwnd, WM_USER + 100, wParam, lParam);

Hope this helps, and I'd appreciate it if someone could explain why this is the case. Also, does every MFC app have the class name "#32770" if anyone knows?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top