Nasty runtime error with WM_PAINT

I have done the basics in creating a simple GUI framework. Set the WNDCLASSEX with lpfnWndProc pointing to a static member function of my base window class. Calling CreateWindowEx with the appropriate parameters returns a valid HWND. This works well for the main form. Its drawn properly, no problems.

When I create a child window and initialize it as mentioned it runs ok; the global WNDPROC routes all messages via the SetWindowLong/GetWindowLong to the window and they are handled ok. ALL BUT THE WM_PAINT MESSAGE. The application crashes with a runtime error. The same occurs with WM_NCPAINT.

Could it be an issue with the paint messages?

LRESULT CALLBACK
WinControl::GlobalWndProc(HWND pHWnd, UINT pMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT
nResult = 0;

WinControl *
nControl = NULL;

MSG
nMsg = { 0 };

WinCriticalSection
nLock;

LPCREATESTRUCT
nLPStruct = { 0 };

nLock.Lock();

nControl = reinterpret_cast<WinControl *>(::GetWindowLong(pHWnd, GWL_USERDATA));

if((nControl == NULL) && (pMsg == WM_NCCREATE))
{
nLPStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);

nControl = reinterpret_cast<WinControl *>(nLPStruct->lpCreateParams);

::SetWindowLong(pHWnd, GWL_USERDATA, (LONG)nControl);
}

if((nControl == NULL) || (!(::IsWindow(nControl->GetHWND()))))
{
nResult = ::DefWindowProc(pHWnd, pMsg, wParam, lParam);
}
else
{
nMsg.hwnd = pHWnd;

nMsg.lParam = lParam;

nMsg.wParam = wParam;

nMsg.message = pMsg;

/////////// THIS PART FAILS WHEN ITS WM_PAINT OR WM_NCPAINT ON THE CHILD WINDOW
/////////// (WS_CHILD | WS_VISIBLE)
nResult = nControl->WndProc(&nMsg);

nControl = NULL;
}

nLock.Unlock();

return nResult;
}

[1789 byte] By [edu.net] at [2008-2-18]