I have this code to get the handle for the console window
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. "
// Set the console screen's title
// m_ConsoleTitle is of type std::string
// Get window Handle
// m_hWnd is of type HWND
m_hWnd = FindWindow(NULL, m_ConsoleTitle.c_str() );
if (m_hWnd == NULL)
DWORD err = GetLastError();
ss <<"FindWindow FAILED, ERROR # " <<err <<"\nhandle = " <<m_hWnd ;
s = ss.str();
MessageBox(0, s.c_str(), "Error", 0);
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?