Weird FindWindow(Ex) errors

This is a discussion on Weird FindWindow(Ex) errors within the Windows Programming forums, part of the Platform Specific Boards category; Code: #include <iostream> #include <fstream> #include <windows.h> using namespace std; int main(int argc, char* argv[]) { HWND taskBar,sysTray,expandTray; int i ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Weird FindWindow(Ex) errors

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
        HWND taskBar,sysTray,expandTray;
        int i = 1;
            
        while ((taskBar = FindWindow("Shell_TrayWnd",""))==NULL)//First argument is the CLASS NAME, Second is the CAPTION of the window
        { 
            Sleep(500);
        } 
        
        printf("%x \n",taskBar);
        while ((sysTray = FindWindowEx(taskBar,0,"TrayNotifyWnd",""))==NULL) 
        {
                Sleep(500);
        }
        printf("%x \n",taskBar);
        while ((expandTray = FindWindowEx(sysTray,0,"Button",""))==NULL)
        {
                Sleep(500);
        }
        printf("%x \n",expandTray);
        PostMessage(expandTray,BM_CLICK,0,0); 
        
        return 0;
    }
    This is some code altered from a snippit a friend showed me that clicked the start bar. I am using Windows Spy, which is a MicroSoft built application that shows all Window classes, and their children, running. This code was supposed to click the "Expand Tray" button in the bottom right, but it never gets passed finding/declaring the first Window, "taskBar'.

    The odd part is, the section to find the "taskBar" window class was never altered from the original snippet, but yet loops forever.

    I have debugged the program, and it indeed loops on the first process. Another strange occurance, when the program runs, the expand tray button disappears, but the tray doesn't expand.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Pass NULL or 0 to the functions FindWindow and FindWindowEx, not an empty string ("")

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Quote Originally Posted by Darryl View Post
    Pass NULL or 0 to the functions FindWindow and FindWindowEx, not an empty string ("")
    The Window class has a blank caption, so "" works
    When you use NULL, the operating system takes it upon itself to select the most relevent result for the first query (in this case, Shell_TrayWnd)

    Regardless, I've tryed it both ways, still a no-go

    News Update: I have added multiple tasks inside the "while" loop, yet none are being executed, and the program is looping indefinitely. Even a simple cout << "test"; is not working. Debug goes to the first process, and then goes back to the While conditional statement.
    Last edited by OpiateDelusion; 08-14-2007 at 10:06 AM.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Requesting to have this moved to Windows API section for more exposure, please

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    As Darryl said,
    Quote Originally Posted by MSDN
    The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings.
    Which means that if you specify "" as the window title, unless the tray window has the exact same title (which may or may not be ""), it will return NULL. Of course MSDN could be wrong, but unless you've explicitly tested this scenerio, I'd tend to follow its direction. When you said you tried using NULL, you meant for the second argument right? Just in case, here is what I/he/MSDN meant:
    Code:
    while((taskBar = FindWindow("Shell_TrayWnd", NULL)) == NULL)
    I use this personally in my own code and it works. On a related note though, I've always hated using this method and cannot understand for the life of me why a better one does not exist (most tuts I've read online use this method too). The huge glaring flaw with this is what happens if Microsoft decides to change the window class for the taskbar window in future versions. It's destined for failure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with DX9 Errors!!!
    By Tommaso in forum Game Programming
    Replies: 7
    Last Post: 06-28-2006, 03:51 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 09:56 AM
  3. Weird Errors in VS 2003
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 07:16 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21