Retrieving Window Handles through TB_GETBUTTON Message.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BlackShadow33p1
    New Member
    • Mar 2008
    • 3

    #1

    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 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);
    
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • BlackShadow33p1
      New Member
      • Mar 2008
      • 3

      #3
      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?

      Comment

      • BlackShadow33p1
        New Member
        • Mar 2008
        • 3

        #4
        K, as far as I can tell right now lpRemoteBuffer is never being written to. Anyone have any ideas as to why?

        Comment

        Working...