468,496 Members | 1,804 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Retrieving Window Handles through TB_GETBUTTON Message.

I'm trying to write a program in C++ that gets the handles of all the visible entries in the windows taskbar. The method I've used so far is to send the TB_GETBUTTON message to the taskbar. Supposedly the window handle for an entry is in the dwData structure in the TBBUTTON structure but I'm having a hard time retrieving the handle from the structure correctly. It seems when I look at the data I've retrieved, it is not a handle (negative value). I think my problem is in the ReadProcessMemory Commands. Anyway, if someone could take a look at this code and offer any suggestions as to fixing this problem I would be very greatful, thanks!


Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <vector>
  4. #include <tchar.h>
  5. #include <stdlib.h>
  6. #include <commctrl.h>
  7.  
  8. LRESULT KillProcess(HWND hWnd);
  9. int GetVisibleWindows();
  10. struct BUTTONDATA
  11.     {
  12.         HWND hwnd;
  13.         UINT uID;
  14.         UINT uCallbackMessage;
  15.         DWORD Reserved[2];
  16.         HICON hIcon;
  17.     };
  18.  
  19. int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  20. {
  21.  
  22. GetVisibleWindows();
  23. return(0);
  24.  
  25. }
  26.  
  27. LRESULT KillProcess(HWND hWnd)
  28. {
  29. LRESULT lRes;
  30. lRes=SendMessage(hWnd,WM_CLOSE,NULL,NULL);
  31. //DestroyWindow(hWnd);
  32. return(lRes);
  33. }
  34.  
  35. int GetVisibleWindows()
  36.  
  37. {
  38.  
  39.     //Declare Variables
  40.  
  41.     TBBUTTON Button;
  42.     BUTTONDATA ent;
  43.     HWND hTask;
  44.     HANDLE hTaskProc;
  45.     HWND hWnd;
  46.     DWORD dwProcessId;
  47.     LPVOID lpRemoteBuffer;
  48.     DWORD lpNumberOfBytesRead;
  49.     int nButton = 0;
  50.  
  51.     //Get handle to TaskBar's ToolBar
  52.  
  53.     hTask = FindWindow("Shell_TrayWnd", NULL);
  54.     hTask = FindWindowEx(hTask, 0, "ReBarWindow32", NULL);
  55.     hTask = FindWindowEx(hTask, 0, "MSTaskSwWClass", NULL);
  56.     hTask = FindWindowEx(hTask, 0, "ToolBarWindow32", NULL);
  57.  
  58.     //Get number of buttons(entries) in the ToolBar(TaskBar)
  59.  
  60.     nButton = SendMessage(hTask, TB_BUTTONCOUNT, NULL, NULL);
  61.  
  62.     //Get TaskBar Process Handle
  63.  
  64.     GetWindowThreadProcessId(hTask, &dwProcessId);
  65.     hTaskProc = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessId);
  66.  
  67.     //Allocate RemoteBuffer
  68.  
  69.     lpRemoteBuffer = VirtualAllocEx(hTaskProc, NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE);
  70.  
  71.     //Get handles for the buttons' windows
  72.  
  73.     for( int index = 0; index < nButton; index++)
  74.     {
  75.  
  76.         SendMessage(hTask, TB_GETBUTTON, index, (LPARAM)lpRemoteBuffer);
  77.         ReadProcessMemory(hTaskProc, lpRemoteBuffer, &Button, sizeof(TBBUTTON), &lpNumberOfBytesRead);
  78.         ReadProcessMemory(hTaskProc, (LPVOID)Button.dwData, &ent, sizeof(BUTTONDATA), &lpNumberOfBytesRead);
  79.         hWnd = ent.hwnd;
  80.  
  81.         //just a little messagebox to see if I got a handle correctly, (and if you run the program you'll see something is wrong by looking at the messagebox.  Any idea of what I'm doing wrong, or a different way to do it?)
  82.  
  83.         TCHAR str[256];
  84.         _stprintf(str, _T("%d"), hWnd);
  85.         MessageBox(NULL,str,"Handle",MB_OK);
  86.  
  87.         //from here-on I still have to develop the rest of my app, so it can be ignored.
  88.  
  89.         for( hWnd = GetFirstChild(hWnd); hWnd; hWnd = GetNextSibling(hWnd) )
  90.         {
  91.  
  92.             //only store visible entries
  93.  
  94.             if(IsWindowVisible(hWnd))
  95.             {
  96.  
  97.                 //Store the window handle in an array
  98.  
  99.  
  100.             }
  101.  
  102.         }
  103.  
  104.     }
  105.  
  106.     return (0);
  107.  
  108.  
Mar 23 '08 #1
3 7610
weaknessforcats
9,207 Expert Mod 8TB
Handles are just addresses so its not hard otr imagine that one might appear as a negatibe number.

However, when Win32 calls fail, you can call GetLastError() to see what the exact failure is. You would call GetLastError() when you Win32 function return zero.

I don't see any checks in your code to insure the Win32 calls worked.
Mar 23 '08 #2
yeah I still have to add in all my checks for errors. So far GetLastError() hasn't been much help, but I'll see If I can figure anything else out. Any ideas as to what is wrong with my code?
Mar 23 '08 #3
K, as far as I can tell right now lpRemoteBuffer is never being written to. Anyone have any ideas as to why?
Mar 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Steve Wark | last post: by
reply views Thread by Steve | last post: by
7 posts views Thread by John | last post: by
2 posts views Thread by Jonathan Boivin | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.