   Failure to create a windows handle
  This seems like a very simple newb error, but I can't seem to find the problem. The problem starts approximately at InitInstance() methodwhen I call the Createwindow() function, it returns a Error, expression cannot be evaluted for my hWndTherefore everything else fails. Again, it starts initInstance(); I marked it in the code// OEngine.cpp : Defines the entry point for the application.//#include "stdafx.h"#include "OEngine.h"#include <gl\gl.h>// Header File For The OpenGL32 Library#include <gl\glu.h>// Header File For The GLu32 Library//#include <gl\glaux.h>// Header File For The GLaux Librarystatic PIXELFORMATDESCRIPTOR pfd ={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,24,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,PFD_MAIN_PLANE,0,0,0,0};#define MAX_LOADSTRING 100// Global Variables:HINSTANCE hInst;// current instanceTCHAR szTitle[MAX_LOADSTRING];// The title bar textTCHAR szWindowClass[MAX_LOADSTRING];// the main window class nameHGLRC hRC=NULL;// Permanent Rendering ContextHDC   hDC=NULL;// Private GDI Device ContextHWND  hWnd=NULL;// Holds Our Window Handleboolkeys[256];// Array Used For The Keyboard Routineboolactive=TRUE;// Window Active Flag Set To TRUE By Defaultboolfullscreen=TRUE;// Fullscreen Flag Set To Fullscreen Mode By DefaultGLvoid ReSizeGLScene(GLsizei width, GLsizei height);int InitGL(GLvoid);int DrawGLScene(GLvoid);BOOL CreateGLWindow(char* title, int width, int height, int bits, BOOL fullscreenflag);GLvoid KillGLWindow(GLvoid);// Forward declarations of functions included in this code module:ATOMMyRegisterClass(HINSTANCE hInstance);BOOLInitInstance(HINSTANCE, int);LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR    lpCmdLine, int  nCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine);hInst = hInstance; // TODO: Place code here.MSG msg;HACCEL hAccelTable;// Initialize global stringsLoadString(hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInst, IDC_OENGINE, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInst);// Perform application initialization:if (!InitInstance (hInst, nCmdShow)){return FALSE;}hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(IDC_OENGINE));if(!(hDC = GetDC(hWnd))){KillGLWindow();MessageBox(NULL, "Failed to Create GL Device Context","ERROR",MB_OK | MB_ICONEXCLAMATION);return false;}int PixelFormat;if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd))){KillGLWindow();MessageBox(NULL, "Cant find suitable pixel format","ERROR",MB_OK | MB_ICONEXCLAMATION);return false;}if(!(hRC=wglCreateContext(hDC))){KillGLWindow();MessageBox(NULL, "Cant create a GL Render Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);return false;}// Main message loop:while (GetMessage(&msg, NULL, 0, 0)){if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}KillGLWindow();return (int) msg.wParam;}GLvoid ReSizeGLScene(GLsizei width, GLsizei height)// Resize And Initialize The GL Window{if (height==0)// Prevent A Divide By Zero By{height=1;// Making Height Equal One}glViewport(0, 0, width, height);// Reset The Current ViewportglMatrixMode(GL_PROJECTION);// Select The Projection MatrixglLoadIdentity();// Reset The Projection Matrix// Calculate The Aspect Ratio Of The WindowgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);// Select The Modelview MatrixglLoadIdentity();}int InitGL(GLvoid)// All Setup For OpenGL Goes Here{glShadeModel(GL_SMOOTH);glClearColor(0.0f, 0.0f, 0.0f, 0.0f);glClearDepth(1.0f);// Depth Buffer SetupglEnable(GL_DEPTH_TEST);// Enables Depth TestingglDepthFunc(GL_LEQUAL);// The Type Of Depth Test To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculationsreturn 1;}int DrawGLScene(GLvoid)// Here's Where We Do All The Drawing{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear The Screen And The Depth BufferglLoadIdentity();// Reset The Current Modelview Matrixreturn TRUE;// Everything Went OK}GLvoid KillGLWindow(GLvoid)// Properly Kill The Window{if (fullscreen)// Are We In Fullscreen Mode?{ChangeDisplaySettings(NULL,0);// If So Switch Back To The DesktopShowCursor(TRUE);// Show Mouse Pointer}if(hRC){if(!wglMakeCurrent(NULL,NULL)){MessageBox(NULL, "Release of DC and RC failed.", "Shutdown error", MB_OK | MB_ICONINFORMATION);}if(!wglDeleteContext(hRC)){MessageBox(NULL, "Release Rendering Context failed.", "Shutdown error", MB_OK | MB_ICONINFORMATION);}hRC = NULL;}if(hDC && !ReleaseDC(hWnd, hDC)){MessageBox(NULL, "Release Device Context Failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);hDC = NULL;}if(hWnd && !DestroyWindow(hWnd)){MessageBox(NULL, "Release hWnd Failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);hWnd = NULL;}if(UnregisterClass("OpenGL", hInst)){MessageBox(NULL, "Release hInst Failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);hInst = NULL;}}BOOL CreateGLWindow(char* title, int width, int height, int bits, BOOL fullscreenflag){return true;}////  FUNCTION: MyRegisterClass()////  PURPOSE: Registers the window class.////  COMMENTS:////    This function and its usage are only necessary if you want this code//    to be compatible with Win32 systems prior to the 'RegisterClassEx'//    function that was added to Windows 95. It is important to call this function//    so that the application will get 'well formed' small icons associated//    with it.//ATOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc= WndProc;wcex.cbClsExtra= 0;wcex.cbWndExtra= 0;wcex.hInstance= hInstance;wcex.hIcon= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OENGINE));wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= MAKEINTRESOURCE(IDC_OENGINE);wcex.lpszClassName= szWindowClass;wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));return RegisterClassEx(&wcex);}////   FUNCTION: InitInstance(HINSTANCE, int)////   PURPOSE: Saves instance handle and creates main window////   COMMENTS:////   In this function, we save the instance handle in a global variable and//   create and display the main program window.//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);   //Above returns, but returns expression cannot be evaluted.    if (!hWnd)//For some reason, it does not return false here. Yet hWnd is not    {//Note valid. It contains a "Error expression can not be evaluted" inside return FALSE;   }   ShowWindow(hWnd, nCmdShow);   UpdateWindow(hWnd);   return TRUE;}////  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)////  PURPOSE:  Processes messages for the main window.////  WM_COMMAND- process the application menu//  WM_PAINT- Paint the main window//  WM_DESTROY- post a quit message and return////LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;switch (message){case WM_COMMAND:wmId    = LOWORD(wParam);wmEvent = HIWORD(wParam);// Parse the menu selections:switch (wmId){case IDM_ABOUT:DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);break;case IDM_EXIT:DestroyWindow(hWnd);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;}// Message handler for about box.INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){UNREFERENCED_PARAMETER(lParam);switch (message){case WM_INITDIALOG:return (INT_PTR)TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL){EndDialog(hDlg, LOWORD(wParam));return (INT_PTR)TRUE;}break;}return (INT_PTR)FALSE;}
  I don't know what your goal is so this might not be appropriate, but if you are just starting out you might want to look into a window manager like SDL or SFML. You can replace that 500 lines of Win32 initialization with a single line call to something like InitWindow() and then your window is up and ready to go.If you need to learn the Win32 api then maybe someone else can help you, but if you just want to make programs there are far easier ways.   ;  Are you sure you have placed the required strings in your project?Are you sure that these are being loaded?What does MyRegisterClass(hInst) return? Does it succeed?   ;  Try changing all instances of szWindowClass with "Your App Name" and see what that does. I'm sure you have a good reason to load the class name from a resource, but I cannot see it. I don't think I've ever seen that done.Anyway...Also, I would strongly suggest that you rip out the NeHe code and write your own. That code gave me nothing but problems. Once I wrote my own, they stopped.Here is what I do. It is based on NeHe's code, but massively modified, cleaned up, simplified and debugged. Feel free to use it. I threw this together from a 2D OpenGL app I'm making, so you'll want to modify it to suit.#include <windows.h>#include <gl\gl.h>#include <gl\glu.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <math.h>HDChDC;HGLRChRC;HWNDhWnd;HINSTANCEhInstance;LRESULTCALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);void ResizeWindow(int width,int height){if(height == 0)height = 1;glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0,width,height,0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}bool InitGL(void){GLuintPixelFormat;PIXELFORMATDESCRIPTORpfd;pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);pfd.nVersion = 1;pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;pfd.iPixelType = PFD_TYPE_RGBA;pfd.cColorBits = 32;pfd.cRedBits = 0;pfd.cRedShift = 0;pfd.cGreenBits = 0;pfd.cGreenShift = 0;pfd.cBlueBits = 0;pfd.cBlueShift = 0;pfd.cAlphaBits = 0;pfd.cAlphaShift = 0;pfd.cAccumBits = 0;pfd.cAccumRedBits = 0;pfd.cAccumGreenBits = 0;pfd.cAccumBlueBits = 0;pfd.cAccumAlphaBits = 0;pfd.cDepthBits = 0;pfd.cStencilBits = 0;pfd.cAuxBuffers = 0;pfd.iLayerType = PFD_MAIN_PLANE;pfd.bReserved = 0;pfd.dwLayerMask = 0;pfd.dwVisibleMask = 0;pfd.dwDamageMask = 0;PixelFormat = ChoosePixelFormat(hDC,&pfd);SetPixelFormat(hDC,PixelFormat,&pfd);hRC = wglCreateContext(hDC);wglMakeCurrent(hDC,hRC);glClearColor(1.0f,1.0f,1.0f,1.0f);return(true);}bool SetupMainWindow(void){WNDCLASSwc;DWORDdwExStyle;DWORDdwStyle;RECTbounds;hInstance = GetModuleHandle(NULL);wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;wc.lpfnWndProc = (WNDPROC)WndProc;wc.cbClsExtra = 0;wc.cbWndExtra = 0;wc.hInstance = hInstance;wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);wc.hCursor = LoadCursor(NULL,IDC_ARROW);wc.hbrBackground = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));wc.lpszMenuName = NULL;wc.lpszClassName = "Clever App Name";RegisterClass(&wc);bounds.left = 10;bounds.right = 1034;bounds.top = 10;bounds.bottom = 778;dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;dwStyle = WS_OVERLAPPEDWINDOW;AdjustWindowRectEx(&bounds,dwStyle,false,dwExStyle);hWnd = CreateWindowEx(dwExStyle,"Clever App Name","Window Title Goes Here...",dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,0,0,bounds.right - bounds.left,bounds.bottom - bounds.top,NULL,NULL,hInstance,NULL);hDC = GetDC(hWnd);if(InitGL()){ShowWindow(hWnd,SW_SHOW);SetForegroundWindow(hWnd);SetFocus(hWnd);ResizeWindow(1024,768);// Do any app set up stuff here. If anything fails, return falsereturn(true);}return(false);}LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){switch(uMsg){case WM_CLOSE:PostQuitMessage(0);return(0);case WM_SIZE:ResizeWindow(LOWORD(lParam),HIWORD(lParam));return(0);}return DefWindowProc(hWnd,uMsg,wParam,lParam);}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){MSGmsg;BOOLdone;if(!SetupMainWindow())return(0);done = false;while(!done){if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){if(msg.message == WM_QUIT){done = true;}else{TranslateMessage(&msg);DispatchMessage(&msg);}}else{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//Draw stuff...SwapBuffers(hDC);}}wglMakeCurrent(NULL,NULL);wglDeleteContext(hRC);ReleaseDC(hWnd,hDC);DestroyWindow(hWnd);UnregisterClass("Clever App Name",hInstance);return(msg.wParam);}And please use source tags...   ;Quote:Original post by maspeirTry changing all instances of szWindowClass with "Your App Name" and see what that does. I'm sure you have a good reason to load the class name from a resource, but I cannot see it. I don't think I've ever seen that done.Anyway...Also, I would strongly suggest that you rip out the NeHe code and write your own. That code gave me nothing but problems. Once I wrote my own, they stopped.Here is what I do. It is based on NeHe's code, but massively modified, cleaned up, simplified and debugged. Feel free to use it. I threw this together from a 2D OpenGL app I'm making, so you'll want to modify it to suit.*** Source Snippet Removed ***And please use source tags...Yes, I am doing the NeHe code. I am glade you were able to point that out. I was a bit skeptical. The tutorials seem as if they could be a little outdated. What I am trying to empahsize though is the problem does not consist with my openGL code. It begins when I try to initilize a win32 code. I pretty much took the initilize code that VC++ provides and I modified with nEhE code. Now all the sudden, my createWindow fails. I will provide more detail soon.;Quote:Original post by numerical25What I am trying to empahsize though is the problem does not consist with my openGL code. It begins when I try to initilize a win32 code.What I was trying to get at is that your code is overly complicated and obtuse. You have a separate function for things that don't need functions and load unnecessary things from resources that might change, or might not even be there in the future (think user fiddling around with a resource editor). Simplify your code and it will make it easier to track down the problem.My comment about NeHe's code was due to personal experience, my distaste for the way it is written and how outdated it is (as you've noted). Both OpenGL's and Windows' initialization procedurea are really very easy and can be done with few lines of code, as I've shown you. NeHe's code is a train wreck.   