473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving Window Handles through TB_GETBUTTON Message.

3 New Member
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 ReadProcessMemo ry 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 8740
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
BlackShadow33p1
3 New Member
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
BlackShadow33p1
3 New Member
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
2247
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 returned as DataSet. Now, here's the problem. On .NET Framework 1.1, if any rows in the dataset returned contain errors (marked by calling the SetColumnError() method or
2
2015
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 returned as DataSet. Now, here's the problem. On .NET Framework 1.1, if any rows in the dataset returned contain errors (marked by calling the SetColumnError() method or
3
4337
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 clicked opens the second form using the window.open(). On the second form I have a textbox (TextBox1) and a button as well. When the second button is clicked it closes the second window with window.close(). What I need to happen is a refresh to...
1
5402
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 that), the popup window will have also a text box and a button. when the User click on the button the value on the textbox will be send back to the textbox on My_WUC. I hope I was clear off what I want to do. I've been searching for some ideas...
0
1310
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: -2147467259 Message: Error creating window handle. TargetSite: Void CreateHandle(System.Windows.Forms.CreateParams) HelpLink: NULL Source: System.Windows.Forms
7
1138
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 called MainTable; Imports System.Data.OleDb Public Class Form2 Inherits System.Windows.Forms.Form Dim cn As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader
0
1797
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 to itself. It appears that most of these threads where this was discussed have been archived, so I'm bringing this topic up again to offer a couple suggestions on this topic. I noticed that most people use the concept of posting to an iFrame. I...
2
596
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 textboxes containing bill's datas. (I already replaced the labels by painting the text instead.) When I click on my filter button, it clears the current usercontrol bills and load the new ones upon the current chosen filters. The bug happens when
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.