Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does uninitialised return value cause Invalid Window Handle err in CreateWindowEx?
    text
    copied!<p><em>Edit</em>- Added code to do with m_hWndClient and the WndProc that were not originally included. In an attempt to be brief I had incorrectly assumed it was unrelated.</p> <p>After the following is run </p> <pre><code>HWND m_hWndFrame; HWND m_hWndClient; // added in Edit2 ... m_hWndFrame = CreateWindowEx(...) </code></pre> <p><code>m_hWndFrame</code> is NULL and <code>GetLastError</code> gives "Error 1400 - Invalid Window Handle" but this works fine:</p> <pre><code>HWND m_hWndFrame = NULL; HWND m_hWndClient = NULL; // added in Edit2 ... m_hWndFrame = CreateWindowEx(...) </code></pre> <p>My <code>WndProc</code> looks like this:</p> <pre><code>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,&amp;clientRect); s_instance-&gt;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)&amp;clientCreate); return 0 ; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefFrameProc(hwnd,m_hWndClient,uMsg,wParam,lParam); } </code></pre> <p>My project now works (after much hair-tearing) but I don't understand why initialising a variable that is only used to hold a return value should matter. </p> <p>Obviously assuming that a variable is NULL or 0 without initialising and then using or testing the contents (eg <code>if (!m_unitialisedVariable)</code>) is going to end in disaster but why should it matter in this instance? There is no requirement for <code>m_hWndFrame</code> to contain anything in particular before calling 'CreateWindowEx' (at least according to the Help in VS2010) so why should it affect the outcome of 'CreateWindowEx'?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload