Click here to Skip to main content
15,878,809 members
Articles / Multimedia / OpenGL
Tip/Trick

OpenGL Version Check Windows Visual Studio No Dependencies

Rate me:
Please Sign up or sign in to vote.
4.89/5 (9 votes)
2 Oct 2013CPOL1 min read 27.5K   11   6
This fully copy pasteable program shows the version of OpenGL your Windows OS supports.

Introduction

This fully copy pasteable program shows the version of OpenGL your Windows OS supports. It is useful because you don't need to include any libraries or external dependencies other than the ones that Windows already contains. This tip is aimed for Visual Studio users. It creates an OpenGL rendering context, and displays the version number of your supported OpenGL version in a MessageBox for you.

Background

I wrote the same code snippet here, but I thought it would be good to put on CodeProject as well in case someone just quickly wanted to find out their OpenGL version:

Using the Code

Create a new project in Visual Studio, choose Win32 project, choose empty project. Add a new file, main.cpp, copy paste the following code into it and press F5 to run, you should see the version number your Windows OS supports in a message box popup.

C++
#include <windows.h>
#include <GL/GL.h>

#pragma comment (lib, "opengl32.lib")

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, 
             __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
    MSG msg          = {0};
    WNDCLASS wc      = {0}; 
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = L"oglversionchecksample";
    wc.style = CS_OWNDC;
    if( !RegisterClass(&wc) )
        return 1;
    CreateWindowW(wc.lpszClassName,L"openglversioncheck", 
            WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,640,480,0,0,hInstance,0); 

    while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
        DispatchMessage( &msg );

    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_CREATE:
        {
        PIXELFORMATDESCRIPTOR pfd =
        {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
            PFD_TYPE_RGBA,            //The kind of framebuffer. RGBA or palette.
            32,                        //Colordepth of the framebuffer.
            0, 0, 0, 0, 0, 0,
            0,
            0,
            0,
            0, 0, 0, 0,
            24,                        //Number of bits for the depthbuffer
            8,                        //Number of bits for the stencilbuffer
            0,                        //Number of Aux buffers in the framebuffer.
            PFD_MAIN_PLANE,
            0,
            0, 0, 0
        };

        HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

        int  letWindowsChooseThisPixelFormat;
        letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd); 
        SetPixelFormat(ourWindowHandleToDeviceContext,letWindowsChooseThisPixelFormat, &pfd);

        HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
        wglMakeCurrent (ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

        MessageBoxA(0,(char*) glGetString(GL_VERSION), "OPENGL VERSION",0);

        wglDeleteContext(ourOpenGLRenderingContext);
        PostQuitMessage(0);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;

}

Points of Interest

It was interesting to note that just simply calling wgl commands was not enough, and the window had to be created at least in WM_CREATE until the version checkup would work.

History

  • 3.10.2013 + Fixed the OpenGL Wikipedia link missing one letter in the < A > tag
  • 2.10.2013 + First version submitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Tieto
Finland Finland
Now working as software architect in the software industry Smile | :) Hello world.

----
I write minimal and efficient no BS code. More at http://www.janimakinen.com

Specialties: Cross-platform tech, C, C++, Game Programming & Design.

I was born in the middle of Finland into a small farming town. Moved to Helsinki, then to Germany and am now back in Helsinki programming multiple projects.

If you're a junior programmer and reading this, keep hacking, the rewards of our craft manifest in the later stages, and it does get good Smile | :)

Comments and Discussions

 
QuestionConverting to VB.NET Pin
N. Henrik Lauridsen4-Oct-13 1:10
N. Henrik Lauridsen4-Oct-13 1:10 
Hi Jani,
I am trying to convert your code to VB.NET using devolper Fusion Convert C# to VB.NET
"http://www.developerfusion.com/tools/convert/csharp-to-vb" but it gives me an error :
An error occured converting your code, probably due to a syntax error: -- line 6 col 1: EOF expected

Can you help me out please. TIA!
Henrik
AnswerRe: Converting to VB.NET Pin
Jani Mäkinen5-Oct-13 10:02
Jani Mäkinen5-Oct-13 10:02 
GeneralMy vote of 5 Pin
littlewater2-Oct-13 14:07
littlewater2-Oct-13 14:07 
GeneralRe: My vote of 5 Pin
Jani Mäkinen2-Oct-13 22:18
Jani Mäkinen2-Oct-13 22:18 
GeneralRe: My vote of 5 Pin
littlewater8-Oct-13 20:35
littlewater8-Oct-13 20:35 
GeneralRe: My vote of 5 Pin
Jani Mäkinen9-Oct-13 21:55
Jani Mäkinen9-Oct-13 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.