Finding handle for Console Window

This is a discussion on Finding handle for Console Window within the C++ Programming forums, part of the General Programming Boards category; I have this code to get the handle for the console window Code: // Set the console screen's title // ...

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Finding handle for Console Window

    I have this code to get the handle for the console window
    Code:
        // Set the console screen's title
        // m_ConsoleTitle is of type std::string
        SetConsoleTitle(m_ConsoleTitle.c_str() );
     
        // Get window Handle
        // m_hWnd is of type HWND
        m_hWnd = FindWindow(NULL, m_ConsoleTitle.c_str() );
     
        if (m_hWnd == NULL)
        {
            DWORD err = GetLastError();
            std::stringstream ss; 
            std::string s;
            ss <<"FindWindow FAILED, ERROR # " <<err <<"\nhandle = " <<m_hWnd ;
            s = ss.str();
            MessageBox(0, s.c_str(), "Error", 0);
            return FALSE;
        }
    Sometimes it works, sometimes it doesn't. When it fails I get NULL returned for FindWindow(). The documentation states that "If the function fails, the return value is NULL. To get extended error information, call GetLastError. "
    But when I call GetLastError I get '0' which is "The operation completed successfully."

    How is it successful if I get a return value of NULL?

    Is there a better way to get the HWND for the console?
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    GetConsoleWindow
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Well then... I don't know how I overlooked that one.....
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    My guess is that the window title is being updated *asynchronously* to your code. So if your code runs "too fast", the window title may not be updated by the time FindWindow() does it's thing.

    Anyway...GetConsoleWindow().

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding correct window handle
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 02-10-2008, 09:43 AM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 02:22 PM
  4. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

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