I was having some strange problems with my windows app and I finally found out that the handle to my main window, stored in a class, is somehow becoming null. Apparently, something I'm doing is a no-no, but I can't explain it. For example:
Code:
class Winbase
{
public:
//...
private:
HWND m_hwnd;
};
bool Winbase::Init(HINSTANCE hinst, WNDPROC WinProc)
{
//...
HWND m_hwnd = CreateWindowEx(0,m_wc.lpszClassName,"A window",WS_OVERLAPPEDWINDOW|WS_SYSMENU,
0,0,GetSystemMetrics(SM_CXMAXIMIZED),GetSystemMetrics(SM_CYMAXIMIZED),
0,0,hinst,0);
Test();
if(!m_hwnd)
MessageBox(NULL,"Handle is bad","In Init()",MB_OK);
//...
}
void Winbase::Test()
{
if(!m_hwnd)
MessageBox(NULL,"Handle is bad","In Test()",MB_OK);
}
The code above produces the message box titled "In Test()" but not a message box titled "In Init()". It seems the handle is valid only as long as I'm in the Init() function (after CreateWindowEx), but I don't understand why