View Full Version : Troubles with creating a window class C++

03-10-2016, 02:23 AM
Hello, I`m new to OpenGl.
I used code from NeHe lessons and it worked well, but then I tried to move it to separate class. That`s the place where I get troubles.
After moving everything to class I found out that CreateWindowEx returns NULL and, thus, hWnd is pointing nowhere.
if( !( hWnd = CreateWindowEx( dwExStyle, _T("OpenGL"), title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
0, 0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top,
NULL, NULL, hInstance, this ) ) ) {
MessageBox( NULL, L"Window Creation Error.", L"ERROR", MB_OK | MB_ICONEXCLAMATION );
return false;

As far as I know CreateWindowEx returns NULL usually when programm doesn`t handle WM_NCCREATE correctly. In changes I only created 2 new functions to solve trouble with the lpfnWndProc member of the WNDCLASS. I assigned &OpenGLInterface::InitialWndProc to lpfnWndProc.

LRESULT CALLBACK OpenGLInterface::InitialWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
if (Msg == WM_NCCREATE) {
LPCREATESTRUCT create_struct = reinterpret_cast<LPCREATESTRUCT>(lParam);
void *lpCreateParam = create_struct->lpCreateParams;
OpenGLInterface *this_window = reinterpret_cast<OpenGLInterface *>(lpCreateParam);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this_window));
SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&OpenGLInterface::StaticWndProc));
return this_window->WndProc(hWnd, Msg, wParam, lParam);
return DefWindowProc(hWnd, Msg, wParam, lParam);

LRESULT CALLBACK OpenGLInterface::StaticWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
LONG_PTR user_data = GetWindowLongPtr(hWnd, GWLP_USERDATA);
OpenGLInterface *this_window = reinterpret_cast<OpenGLInterface *>(user_data);
return this_window->WndProc(hWnd, Msg, wParam, lParam);

But something went wrong and now it creates a window that doesn`t have connection with keyboard and I even`t can`t draw a line on it.
Debug shows that before SetWindowLongPtr this_window point at OpenGLInterface class as it was expected. Anything else wasn`t changed, thus, I assume that the problem is somewhere here.
Thank you for any help

P. S. I also share links to github commits
In case anyone wants to see the whole code
I`m sorry for comments in russian, they were copied from russian translation of NeHe lessons