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?