473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 8699
weaknessforcats
9,208 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

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

Similar topics

6
by: Programatix | last post by:
Hi, I'm working on a project which includes WebServices and Windows Form application. The Windows Form application will call the WebServices to retrieve data from database. The data will be...
2
by: Programatix | last post by:
Hi, I'm working on a project which includes WebServices and Windows Form application. The Windows Form application will call the WebServices to retrieve data from database. The data will be...
3
by: Steve Wark | last post by:
I have created a ASP.NET application and created two forms within the application (Webform1.aspx & Webform2.aspx). On the first form I have placed a textbox (TextBox1) and a button, which when...
1
by: Jorge Ponte | last post by:
hi I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In that WUC I want have a textbox and a button. I want to click on the button and open a popup (I use javascript for...
0
by: Steve | last post by:
Some end users are getting the following message after being in the application for quite some time. Exception Type: System.ComponentModel.Win32Exception NativeErrorCode: 14 ErrorCode:...
7
by: John | last post by:
After reading the tutorial from http://www.startvbdotnet.com/ado/msaccess.aspx I wrote this code in an attempt to get information from my MDB file called FillMe. The only table in that Access file is...
0
by: homsar | last post by:
I've seen many posts discussing the options we have when using modal windows within a web based application. Often times it seems the greatest hurdle is simply getting the modal window to post back...
2
by: Jonathan Boivin | last post by:
Hi people, Let me introduce to how I get this error. I have a form which load all my bills representation depending upon filters which each bill is a usercontrol of my own having some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.