Thread: FindWindow inconsistency

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    23

    FindWindow inconsistency

    I've been looking into FindWindow and have created the following code to illustrate:

    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    int main()
    {
        if (FindWindow("Windows Task Manager", 0))                  //This doesn't work
        {
            cout << "\n" << "Windows Task Manager \t found" << endl;
        }
        else
        {
            cout << "\n" << "Windows Task Manager \t not found" << endl;
        }
    
        if (FindWindow("Calculator", 0))                            //This doesn't work
        {
            cout << "\n" << "Calculator \t\t found" << endl;
        }
        else
        {
            cout << "\n" << "Calculator \t\t not found" << endl;
        }
    
        if (FindWindow("Notepad", 0))                               //This works
        {
            cout << "\n" << "Notepad \t\t found" << endl;
        }
        else
        {
            cout << "\n" << "Notepad \t\t not found" << endl;
        }
    
        cin.ignore();
        cin.get();
        return 0;
    }
    The executable identifies Notepad correctly as being open. If Calculator or Windows Task Manager are open, neither is identified as such. Why doesn't FindWindow work with all of these windows in a consistent manner? Is there a way of picking up the processes instead (calc.exe, notepad.exe and taskmgr.exe)?

    I realise that the code isn't particularly sensible (several similar if ... else etc.) but I only created it to demonstrate the point I want to ask.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    "Notepad" also happens to be the name of the executable. What happens if you use "calc" as the parameter? (I'm unfamiliar with FindWindow().)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by dwks
    "Notepad" also happens to be the name of the executable. What happens if you use "calc" as the parameter? (I'm unfamiliar with FindWindow().)
    Findwindow looks for the text in the title bar or alternatively the window class.

    I will check out the code above when i get home to see if I get the same results

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
        if (FindWindow(0, "Windows Task Manager"))
        {
            cout << "\n" << "Windows Task Manager \t found" << endl;
        }
        else
        {
            cout << "\n" << "Windows Task Manager \t not found" << endl;
        }
    
        if (FindWindow(0, "Calculator"))
        {
            cout << "\n" << "Calculator \t\t found" << endl;
        }
        else
        {
            cout << "\n" << "Calculator \t\t not found" << endl;
        }
    http://msdn.microsoft.com/library/de...findwindow.asp

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What happens if you run it on a German computer?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    23
    dwks - I've tried using the name of the executable, rather than the window title. It still doesn't pick up Calculator or Windows Task Manager.

    Tonto - I'm aware of the MSDN site that you quoted but I'm afraid that it doesn't really mean a lot to me at this stage.

    I'm still not sure why it works with Notepad but not the others.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    According to the given link, the first parameter is the window class name, the second is the title (in the titlebar). So this should pick up task manager:
    Code:
    if (FindWindow(NULL, "Windows Task Manager"))
    Apparently notepad has the same classname...

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    23
    Perfect - thank you.

    I knew there had to be something different about Notepad which caused the inconsistency.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program inconsistency
    By bchan90 in forum C Programming
    Replies: 4
    Last Post: 12-09-2008, 02:57 PM
  2. Inconsistency detected
    By fidodidohk in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2007, 05:12 AM
  3. Geting "hwnds" without FindWindow
    By lala123 in forum Windows Programming
    Replies: 16
    Last Post: 08-15-2005, 07:07 PM
  4. Findwindow Sendmessage Help
    By Coder87C in forum Windows Programming
    Replies: 26
    Last Post: 08-12-2003, 01:28 AM
  5. FindWindow()
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 05-27-2002, 11:53 AM