title: Win32: Modal dialog not returning focus
body: <p>I'm writing a win32 wrapper classes, mainly to learn more about win32 programming.
  To get around the problem of c-style callbacks, the following method stores/retrieves the pointer using SetWindowLong/GetWindowLong and passes it to the actual winproc.</p>
  
  <pre><code>LRESULT CALLBACK WinClass::WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
      // On window creation, WindowProc receives lParam as a LPCREATESTRUCT 
      // Store *this* pointer as long in GWL_USERDATA
      if (msg == WM_NCCREATE)
          ::SetWindowLong(hwnd, GWL_USERDATA, reinterpret_cast&lt;long&gt;(reinterpret_cast&lt;LPCREATESTRUCT&gt;(lParam)-&gt;lpCreateParams));
  
      // Retrieve the pointer
      WinClass *wnd = reinterpret_cast&lt;WinClass*&gt;(::GetWindowLongPtr(hwnd, GWL_USERDATA));
  
      // Call the actual winproc function
      if (wnd)
          return wnd-&gt;WndProc(hwnd, msg, wParam, lParam);
      // Default to DefWindowProc message handler function
      return ::DefWindowProc(hwnd, msg, wParam, lParam);
  }
  </code></pre>
  
  <p>Winclass is the class wrapping the main window created by CreateWindowEx. The same WindowProc function is part of the MDlgClass wrapping the modal dialog. I'm calling the dialog like this</p>
  
  <pre><code>DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(id), hwnd, DialogProc);
  </code></pre>
  
  <p>If I pass NULL as the hWndParent, the dialog works okay as a modeless dialog but if I pass hwnd, the handle to the main window as hWndParent the dialog works correctly as a modal dialog. However when i close the dialog it doesnt pass control back to the main parent window? Debugging in Visual Studio shows its hanging in the message pump in WinMain.</p>
  
  <p>I thought of using a hashmap to map the pointers but I'd rather do it using GetWindowLong etc. Is this possible? I've tried storing the dialog pointer in DWL_USER but it doesnt help.</p>
  
  <p>Any help would be appreciated, I'm still getting my head around Win32.</p>
  
  <p>EDIT: I'm destroying the dialog using EndDialog</p>
  
  <p>EDIT: I'm storing the pointer in the GWL_USERDATA region of the main window, which is not used by windows and I'm only modifying it in WinClass::WindowProc when the window is first created. If I don't instantiate a dialog class, I know the pointer is being accessed correctly since the application responds to menu commands processed via WindowProc and WM_COMMAND.</p>