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!
Code:
#include <windows.h>
#include <windowsx.h>
#include <vector>
#include <tchar.h>
#include <stdlib.h>
#include <commctrl.h>
LRESULT KillProcess(HWND hWnd);
int GetVisibleWindows();
struct BUTTONDATA
{
HWND hwnd;
UINT uID;
UINT uCallbackMessage;
DWORD Reserved[2];
HICON hIcon;
};
int _stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
GetVisibleWindows();
return(0);
}
LRESULT KillProcess(HWND hWnd)
{
LRESULT lRes;
lRes=SendMessage(hWnd,WM_CLOSE,NULL,NULL);
//DestroyWindow(hWnd);
return(lRes);
}
int GetVisibleWindows()
{
//Declare Variables
TBBUTTON Button;
BUTTONDATA ent;
HWND hTask;
HANDLE hTaskProc;
HWND hWnd;
DWORD dwProcessId;
LPVOID lpRemoteBuffer;
DWORD lpNumberOfBytesRead;
int nButton = 0;
//Get handle to TaskBar's ToolBar
hTask = FindWindow("Shell_TrayWnd", NULL);
hTask = FindWindowEx(hTask, 0, "ReBarWindow32", NULL);
hTask = FindWindowEx(hTask, 0, "MSTaskSwWClass", NULL);
hTask = FindWindowEx(hTask, 0, "ToolBarWindow32", NULL);
//Get number of buttons(entries) in the ToolBar(TaskBar)
nButton = SendMessage(hTask, TB_BUTTONCOUNT, NULL, NULL);
//Get TaskBar Process Handle
GetWindowThreadProcessId(hTask, &dwProcessId);
hTaskProc = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessId);
//Allocate RemoteBuffer
lpRemoteBuffer = VirtualAllocEx(hTaskProc, NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE);
//Get handles for the buttons' windows
for( int index = 0; index < nButton; index++)
{
SendMessage(hTask, TB_GETBUTTON, index, (LPARAM)lpRemoteBuffer);
ReadProcessMemory(hTaskProc, lpRemoteBuffer, &Button, sizeof(TBBUTTON), &lpNumberOfBytesRead);
ReadProcessMemory(hTaskProc, (LPVOID)Button.dwData, &ent, sizeof(BUTTONDATA), &lpNumberOfBytesRead);
hWnd = ent.hwnd;
//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?)
TCHAR str[256];
_stprintf(str, _T("%d"), hWnd);
MessageBox(NULL,str,"Handle",MB_OK);
//from here-on I still have to develop the rest of my app, so it can be ignored.
for( hWnd = GetFirstChild(hWnd); hWnd; hWnd = GetNextSibling(hWnd) )
{
//only store visible entries
if(IsWindowVisible(hWnd))
{
//Store the window handle in an array
}
}
}
return (0);
}
Comment