Skip to main content
GameDev.net gamedev.net
🔒 Locked

wglCreateContext() access violation

Started by Mantear Sep 4, 2008 at 8:06 AM 0 replies 1.8k views
Original Post
Mantear
Mantear
Greetings, I fired up my Visual Studio this morning and tried to run my OpenGL code, only to find I'm getting an access violation on the call to wglCreateContext(). I've used this initialization code for a very long time, so I have no idea what could have happened to cause this. Any ideas? The full source up to the error is below. Scroll to the bottom to see where it occurs.
bool OGLWindow::CreateOGLWindow(std::string title)
{
    unsigned int PixelFormat;   // Holds the results after searching for a match
    WNDCLASS    wc;             // Windows Class Structure
    DWORD       dwExStyle;      // Window Extended Style
    DWORD       dwStyle;        // Window Style
    RECT        WindowRect;     // Grabs Rectangle Upper Left / Lower Right Values

    WindowTitle = title;

    WindowRect.left     = (long)0;              // Set Left Value To 0
    WindowRect.right    = (long)(Viewports[0])[2];    // Set Right Value To Requested Width
    WindowRect.top      = (long)0;              // Set Top Value To 0
    WindowRect.bottom   = (long)Viewports[0][3];    // Set Bottom Value To Requested Height

    hInstance           = GetModuleHandle(NULL);				// Grab An Instance For Our Window
    wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Move, And Own DC For Window
    wc.lpfnWndProc      = (WNDPROC) WndProc;					// WndProc Handles Messages
    wc.cbClsExtra       = 0;									// No Extra Window Data
    wc.cbWndExtra       = 0;									// No Extra Window Data
    wc.hInstance        = hInstance;							// Set The Instance
    wc.hIcon            = LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
    wc.hbrBackground    = NULL;									// No Background Required For GL
    wc.lpszMenuName	    = NULL;									// We Don't Want A Menu
    wc.lpszClassName    = WindowClassName.c_str();			    // Set The Class Name

    // attempt to register the window class
    if (!RegisterClass(&wc))
    {
        LOG("Failed To Register The Window Class", LOG_ERROR, LOG_HIGH);

        // exit and return false
        return false;
    }

    // attempt Fullscreen mode?
    if (Fullscreen)
    {
        DEVMODE dmScreenSettings;								// Device Mode
        memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared
        dmScreenSettings.dmSize = sizeof(dmScreenSettings);		// Size Of The Devmode Structure
        dmScreenSettings.dmPelsWidth	= Viewports[0][2]; 			// Selected Screen Width
        dmScreenSettings.dmPelsHeight	= Viewports[0][3];			// Selected Screen Height
        dmScreenSettings.dmBitsPerPel	= ResolutionBit;		// Selected Bits Per Pixel
        dmScreenSettings.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

        // Try to set selected mode and get results.
        // NOTE: CDS_FULLSCREEN gets rid of start bar.
        if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
        {
            // If the mode fails, offer two options.  Quit or run in a window.
            if (MessageBoxA(NULL, "The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?", "NeHe GL", MB_YESNO|MB_ICONEXCLAMATION) == IDYES)
            {
                // Select windowed mode
                Fullscreen = false;
            }
            else
            {
                // Pop up a message box letting the user know the program is closing
                MessageBoxA(NULL, "Program Will Now Close.", "ERROR", MB_OK | MB_ICONSTOP);

                // exit and return false
                return false;
            }
        }
    }

    // Are we still in Fullscreen mode?
    if (Fullscreen)
    {
        // Window Extended Style
        dwExStyle = WS_EX_APPWINDOW;
        // Windows Style
        dwStyle = WS_POPUP;
        // Show mouse pointer
        ShowCursor(true);
    }
    else
    {
        // Window Extended Style
        dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
        // Windows Style
        dwStyle = WS_OVERLAPPEDWINDOW;
        // Show mouse pointer
        ShowCursor(true);
    }

    // Adjust Window To True Requested Size
    AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle);

    // Create our window
    if (!(hWnd = CreateWindowEx(dwExStyle,							// Extended Style For The Window
                                WindowClassName.c_str(),			// Class Name
                                WindowTitle.c_str(),				// Window Title
                                WS_CLIPSIBLINGS |					// Required Window Style
                                WS_CLIPCHILDREN |					// Required Window Style
                                dwStyle,							// Selected Window Style
                                0, 0,								// Window Position
                                WindowRect.right-WindowRect.left,	// Calculate Adjusted Window Width
                                WindowRect.bottom-WindowRect.top,	// Calculate Adjusted Window Height
                                NULL,								// No Parent Window
                                NULL,								// No Menu
                                hInstance,							// Instance
                                NULL)))								// Don't Pass Anything To WM_CREATE
    {
        // Window not properly created
        // Reset the display
        DestroyOGLWindow();

        LOG("Window Creation Error", LOG_ERROR, LOG_HIGH);

        // Return false
        return false;
    }

    // Describe a pixel format
    // pfd tells windows how we want things to be
    static PIXELFORMATDESCRIPTOR pfd =	
    {
        sizeof(PIXELFORMATDESCRIPTOR),	// Size Of This Pixel Format Descriptor
        1,								// Version Number
        PFD_DRAW_TO_WINDOW |			// Format Must Support Window
        PFD_SUPPORT_OPENGL |			// Format Must Support OpenGL
        PFD_DOUBLEBUFFER,				// Must Support Double Buffering
        PFD_TYPE_RGBA,					// Request An RGBA Format
        static_cast<BYTE>(ResolutionBit),  				// Select Our Color Depth
        0, 0, 0, 0, 0, 0,				// Color Bits Ignored
        0,								// No Alpha Buffer
        0,								// Shift Bit Ignored
        0,								// No Accumulation Buffer
        0, 0, 0, 0,						// Accumulation Bits Ignored
        16,								// 16Bit Z-Buffer (Depth Buffer)
        0,								// No Stencil Buffer
        0,								// No Auxiliary Buffer
        PFD_MAIN_PLANE,					// Main Drawing Layer
        0,								// Reserved
        0, 0, 0							// Layer Masks Ignored
    };
	
    // Did we get a device context?
    if (!(hDC = GetDC(hWnd)))
    {
        // Reset the display
        DestroyOGLWindow();

        LOG("Can't Create A GL Device Context", LOG_ERROR, LOG_HIGH);

        // Return false
        return false;
    }

    // Did windows find a matching pixel format?
    if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
    {
        // Reset the display
        DestroyOGLWindow();

        LOG("Can't Find A Suitable PixelFormat", LOG_ERROR, LOG_HIGH);

        // Return false
        return false;
    }

    // Are we able to set the pixel format?
    if(!SetPixelFormat(hDC, PixelFormat, &pfd))
    {
        // Reset the display
        DestroyOGLWindow();

        LOG("Can't Set The PixelFormat", LOG_ERROR, LOG_HIGH);

        // Return false
        return false;
    }

    // Are we able to get a rendering context?
    if (!(hRC = wglCreateContext(hDC))) // <--- Access violation on this call
    {
        // Reset the display
        DestroyOGLWindow();

        LOG("Can't Create A GL Rendering Context", LOG_ERROR, LOG_HIGH);

        // Return false
        return false;
    }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.