Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Timers and hooks - (http://www.programmingforums.org/showthread.php?t=15038)

pcbrainbuster Jan 25th, 2008 9:24 PM

Timers and hooks -
 
Sup guys,

Why is it that timers remove your hooks(in my case a mouse hook(WH_MOUSE_LL))?

Thanks.

null_ptr0 Jan 26th, 2008 9:12 AM

Re: Timers and hooks -
 
They do?
I thought you'd just have to call UnhookWindowsHook(Ex).
Oh, and thats a low level mouse hook, not just a mouse hook.

Ancient Dragon Jan 26th, 2008 10:01 AM

Re: Timers and hooks -
 
Quote:

Originally Posted by pcbrainbuster (Post 140299)
Why is it that timers remove your hooksThanks.

Most likely because you wrote some crappy code that is destroy memory somewhere.

pcbrainbuster Jan 26th, 2008 1:09 PM

Re: Timers and hooks -
 
Quote: Crappy
:suprised: Harsh... :(

Hmm, according to the replies I think that timers don't remove hooks. Well I guess I'll revise my code and see what went wrong... It may be crappy code...

Thanks for now.

pcbrainbuster Jan 26th, 2008 2:15 PM

Re: Timers and hooks -
 
It took me some time but here is the offending code(the following code is what is causing problems) -

:

  1.     case 202 :
  2.  
  3.     if(CurWndNo != 0)
  4.     {
  5.  
  6.       for(CurWndNo = WndList.size(); CurWndNo > 0; CurWndNo--)
  7.       {
  8.  
  9.       DeleteMenu(WindowList, 0, MF_BYPOSITION);
  10.  
  11.       }
  12.  
  13.     }
  14.  
  15.     WndList.clear();
  16.  
  17.     strcpy(WindowChoice, "Wnd List");
  18.     EnumWindows((WNDENUMPROC)&EnumProc, NULL);
  19.  
  20.     for(CurWndNo = 0; CurWndNo < WndList.size(); CurWndNo++)
  21.     {
  22.  
  23.       GetWindowText(WndList[CurWndNo], WndTitle, GetWindowTextLength(WndList[CurWndNo]) + 1);
  24.       AppendMenu(WindowList, MF_STRING, NULL, WndTitle);
  25.  
  26.     }
  27.  
  28.     break;


Do you see any problems?

Thanks.

Ancient Dragon Jan 26th, 2008 6:45 PM

Re: Timers and hooks -
 
Quote:

:suprised: Harsh...
Oh so you don't think so ??? But don't feel too bad, I've written some crappy code too :)

line 2: why are you using numbers like that in the switch statement instead of the pre-defined macros from windows.h? It might work for now, but there is no guarentee those values won't change in the future.

line 9: why are you deleting the same menu multiple times? The first parameter is supposed to be HWND, don't know what WindowList is, but its value never changes inside that loop. Its possible that is whats producing the problem -

line 19: remove the & from in front of WinProc. Functions are always passed by reference so the & symbol is not needed.

line 20: what is the value of WndList.size() at that point? Answer: 0 because it was emptied on line 15. Consequently lines 23 and 24 will never get executed.

pcbrainbuster Jan 26th, 2008 9:50 PM

Re: Timers and hooks -
 
Well actually this is how I get all the open windows into a menu...

Quote: remove the & from in front of WinProc. Functions are always passed by reference so the & symbol is not needed.
R. I didn't know that, thanks!

Quote: line 20: what is the value of WndList.size() at that point? Answer: 0 because it was emptied on line 15. Consequently lines 23 and 24 will never get executed.
Maybe things will clear up if you have a look at this -

:

  1. WndList.push_back(hWnd2);


That's basically what is happening in EnumWindows(...). Every three seconds(202, this is in WM_TIMER - which is why I am using a number) a menu called WindowList lists all the windows in the taskbar. You should now be able to see why I use WndList.clear() to clear all the windows in the array; to get a fresh list of new ones.

Quote: line 9: why are you deleting the same menu multiple times? The first parameter is supposed to be HWND, don't know what WindowList is, but its value never changes inside that loop. Its possible that is whats producing the problem -
R. Soory, the first parameter is HMENU :)

Any ideas?

Thanks.

Ancient Dragon Jan 26th, 2008 10:43 PM

Re: Timers and hooks -
 
Ok I understandand now about that WinList problem.

>>R. Soory, the first parameter is HMENU
Right, but the problem still exists. The value of that variable still doesn't change between loop iterations. My suggestion is to remove the loop around that line so that it gets executed only once. Sending an invalid HMENU to that function might produce undesireable and unpredictable results.

pcbrainbuster Jan 27th, 2008 8:02 AM

Re: Timers and hooks -
 
But WndList is being changed in EnumWindows(), I think I should mension that WndList is a vector<HWND> and in EnumWindows() I am appending handles to windows...

Any ideas?

Thanks.

Ancient Dragon Jan 27th, 2008 10:51 AM

Re: Timers and hooks -
 
are you deaf or something? you still don't get it? We are not talking about WinList but WindowList
:

      for(CurWndNo = WndList.size(); CurWndNo > 0; CurWndNo--)
      {

      DeleteMenu(WindowList, 0, MF_BYPOSITION);

      }

That loop passes the same argument to DeleteMenu() on every iteration. WindowList never changes.


All times are GMT -5. The time now is 3:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC