title: Encapsulating WndProc Problem
body: <p>Okay so i wrote a basic class to do encapsulation of a win32 window. I ended up creating a static router callback function to route the messages into another function of the class.</p>
  
  <p><strong>EDIT OKAY I Got it</strong></p>
  
  <pre><code>m_MainWindowHandle = CreateWindowEx(
      windowFormat, 
      L"LUDO ENGINE", 
      m_WindowName.c_str(), 
      WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT,
      0,
      m_WindowDimensions.first,
      m_WindowDimensions.second,
      NULL, 
      NULL,
      m_EngineInstance,
      (void*)this);
  </code></pre>
  
  <p>I forgot to pass in the (void*)this pointer. AJKFHDJKDF</p>
  
  <pre><code>LRESULT CALLBACK Test::EngineBase::WndRouter(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  {
  
  
      Test::EngineBase *base = NULL;
  
      if(message == WM_NCCREATE)
      {
          base = reinterpret_cast&lt;Test::EngineBase*&gt;((LONG)((LPCREATESTRUCT)lParam)-&gt;lpCreateParams);
          SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR)base);
      }
      else
      {
          base = reinterpret_cast&lt;Test::EngineBase*&gt;(GetWindowLongPtr(hWnd, GWL_USERDATA));
      }
  
      if(base == NULL)
      {
          return DefWindowProc(hWnd, message, wParam, lParam);
      }
      base-&gt;SetHWnd(hWnd);
      return base-&gt;WndProc(hWnd, message, wParam, lParam);
  }
  </code></pre>
  
  <p>Which calls this function:</p>
  
  <pre><code>LRESULT CALLBACK Test::EngineBase::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  {
      int wmId, wmEvent;
  
      switch (message)
      {
      case WM_KEYDOWN:
          m_InputManager-&gt;GetKeyboard()-&gt;SetKeyPressed(static_cast&lt;int&gt;(wParam));
          break;
      case WM_KEYUP:
          m_InputManager-&gt;GetKeyboard()-&gt;SetKeyUnpressed(static_cast&lt;int&gt;(wParam));
          break;
      case WM_MOUSEMOVE:
      case WM_NCMOUSEMOVE:
          m_InputManager-&gt;GetMouse()-&gt;SetMouseMouse(LOWORD(lParam),HIWORD(lParam));
          break;
      case WM_MOUSEWHEEL:
          break;
      case WM_COMMAND:
          wmId    = LOWORD(wParam);
          wmEvent = HIWORD(wParam);
          // Parse the menu selections:
          switch (wmId)
          {
              //case IDM_ABOUT:
              //    //DialogBox(m_EngineInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
              //    //break;
          case IDM_EXIT:
              DestroyWindow(hWnd);
              break;
          default:
              return DefWindowProc(hWnd, message, wParam, lParam);
          }
          break;
      case WM_DESTROY:
          PostQuitMessage(0);
          break;
      default:
          return DefWindowProc(hWnd, message, wParam, lParam);
      }
      return 0;
  }
  </code></pre>
  
  <p>Then i inherit from EngineBase. Now WndRouter always gets called, instead always results in <code>base == NULL</code>, so it never actually calls the WndProc function.</p>
  
  <p>What am i doing wrong?</p>
  
  <p><em>EDIT</em></p>
  
  <p>Heres how i get the intial messages into the engine:</p>
  
  <pre><code>while (returnValue == 9999)
  {
      engine-&gt;Update();
      while (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
      {
          if (msg.message == WM_QUIT)
          {                
              returnValue = static_cast&lt;int&gt;(msg.wParam);
          }
  
          TranslateMessage(&amp;msg);
          DispatchMessage(&amp;msg);
      }
  
  }
  </code></pre>
  
  <p>Heres The header:</p>
  
  <pre><code>namespace Test
  {   
      //forward defines
      namespace Input
      {
          class InputManager;
      }
  
  
      // main classs
      class EngineBase
      {
      protected:
          virtual HRESULT SetKeyBindings();
          virtual HRESULT LoadResources();
  
          static LRESULT CALLBACK WndRouter(HWND hWnd,  UINT message,  WPARAM wParam,  LPARAM lParam);
          virtual LRESULT CALLBACK WndProc(HWND hWnd,  UINT message,  WPARAM wParam,  LPARAM lParam);
  
          // Use reference counting schematics to prevent deletion.
          boost::shared_ptr&lt;Test::Input::InputManager&gt; m_InputManager;
      public:
          EngineBase(__in const HINSTANCE engineHandleInstance);
          ~EngineBase();
  
          // Engine Specific things
          HRESULT InitializeEngine(__in const int nCmdShow, __in const std::pair&lt;INT, INT&gt;&amp; windowDimensions, __in const Test::String&amp; windowName);
          HRESULT Register(__in const WNDCLASSEX&amp; windowDefs);
  
          // Game Specific things
          virtual HRESULT Initialize() = 0;
          virtual HRESULT Update();
  
          inline void SetHWnd(__in const HWND windowHandle)
          {
              m_MainWindowHandle = windowHandle;
          }
  
          inline const std::pair&lt;INT, INT&gt; GetWindowDimensions()
          {
              return m_WindowDimensions;
          }
  
      private:
          HRESULT InitializeSubSystems();
  
          // Win32 stuff
          HRESULT CheckForOtherInstance();
          WORD    RegisterEngineClass();
          WNDCLASSEX  m_WindowClass;
          HWND        m_MainWindowHandle;
          HINSTANCE   m_EngineInstance;
          HANDLE      m_Mutex;
  
          std::pair&lt;INT, INT&gt; m_WindowDimensions;
          Test::String m_WindowName;
      };
  }
  </code></pre>
  
  <p><em>EDIT</em> This is how i set my windProc def</p>
  
  <p>In main.cpp</p>
  
  <pre><code>WNDCLASSEX windowClass;
  windowClass.cbSize = sizeof(WNDCLASSEX);
  windowClass.style = NULL;
  windowClass.cbClsExtra = 0;
  windowClass.cbWndExtra = 0;
  windowClass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(1));
  windowClass.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE(2));
  windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  windowClass.lpszMenuName = NULL;
  windowClass.lpszClassName = L"ENGINE";
  
  if(FAILED(engine-&gt;Register(windowClass)))
  {
      return FAIL;
  }
  </code></pre>
  
  <p>Register does this:</p>
  
  <pre><code>HRESULT Test::EngineBase::Register(__in const WNDCLASSEX&amp; windowDefs)
  {
      HRESULT hr = S_OK;
      m_WindowClass = windowDefs;
      m_WindowClass.lpfnWndProc = WndRouter;
      m_WindowClass.hInstance = m_EngineInstance;
  
      if(!RegisterClassEx(&amp;m_WindowClass))
      {
          hr = E_CANNOT_CREATE_WINDOW_CLASS;
          return hr;
      }
      return hr;
  }
  </code></pre>