c++ - Why does uninitialised return value cause Invalid Window Handle err in CreateWindowEx? -


edit- added code m_hwndclient , wndproc not included. in attempt brief had incorrectly assumed unrelated.

after following run

hwnd m_hwndframe; hwnd m_hwndclient; // added in edit2 ... m_hwndframe = createwindowex(...) 

m_hwndframe null , getlasterror gives "error 1400 - invalid window handle" works fine:

hwnd m_hwndframe = null; hwnd m_hwndclient = null; // added in edit2 ... m_hwndframe = createwindowex(...) 

my wndproc looks this:

lresult callback programmanager::wndproc(hwnd hwnd, uint umsg, wparam wparam, lparam lparam) {     clientcreatestruct  clientcreate;     hinstance hinstance = getmodulehandle(null);     rect clientrect;      switch (umsg)     {     case wm_create:                    clientcreate.hwindowmenu  = null;         clientcreate.idfirstchild = idm_firstchild ;         getclientrect(hwnd,&clientrect);          s_instance->m_hwndclient = createwindowex(ws_ex_clientedge, text ("mdiclient"), null,             ws_child | ws_clipchildren | ws_clipsiblings, 0, 0, clientrect.right,             clientrect.bottom, hwnd, (hmenu)id_mdi_client, hinstance,              (lpvoid)&clientcreate);           return 0 ;      case wm_close:         destroywindow(hwnd);         break;     case wm_destroy:         postquitmessage(0);         break;     }      return defframeproc(hwnd,m_hwndclient,umsg,wparam,lparam); } 

my project works (after hair-tearing) don't understand why initialising variable used hold return value should matter.

obviously assuming variable null or 0 without initialising , using or testing contents (eg if (!m_unitialisedvariable)) going end in disaster why should matter in instance? there no requirement m_hwndframe contain in particular before calling 'createwindowex' (at least according in vs2010) why should affect outcome of 'createwindowex'?

the problem not lie fact m_hwndframe or not null rather whether m_hwndclient or not null.

in wm_create handler in wndproc mdi client window created , handle stored in m_hwndclient. unprocessed messages go through line @ end of wndproc:

return defframeproc(hwnd,m_hwndclient,umsg,wparam,lparam); 

however wm_create not first message sent window (wm_nccreate gets sent before wm_create). when message received before wm_create, m_hwndclient still uninitialised , invalid window handle indicated error message.

so initialising m_hwndframe isn't technically necessary in instance initialising m_hwndclient otherwise defframeproc call gets garbage client window's handle.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -