Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL context without opening a window - wglMakeCurrent fails with HDC and HGLRC when using HWND made with GetDesktopWindow
    primarykey
    data
    text
    <p>This is somewhat a duplicate of <a href="https://stackoverflow.com/questions/576896/can-you-create-opengl-context-without-opening-a-window">this question</a>.</p> <p>I am trying to make a windowless console application to check up on OpenGL version supported. In order to do this I need to set up a render context - but without creating a window. I am trying to use desktop handle, which I won't write to.</p> <p>I forgot to set pixel format in previous example - that is probable reason why creation of render context failed - however even with pixel format set, I cannot activate it. wglMakeCurrent(hDC, hRC) just returns 0.</p> <p>Here is the complete source code dump:</p> <pre><code> #include &lt;iostream&gt; #include &lt;GL/GLee.h&gt; #include &lt;windows.h&gt; HDC hDC = NULL; HGLRC hRC = NULL; HWND hWnd = NULL; HINSTANCE hInstance; int res = 0; int pf = 0; PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, /* version */ PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, /* 24-bit color depth */ 0, 0, 0, 0, 0, 0, /* color bits */ 0, /* alpha buffer */ 0, /* shift bit */ 0, /* accumulation buffer */ 0, 0, 0, 0, /* accum bits */ 32, /* z-buffer */ 0, /* stencil buffer */ 0, /* auxiliary buffer */ PFD_MAIN_PLANE, /* main layer */ 0, /* reserved */ 0, 0, 0 /* layer masks */ }; std::string trash; int main(int argc, char**argv) { hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window hWnd = GetDesktopWindow(); // Instead of CreateWindowEx if (!(hDC = GetDC(hWnd))) { std::cout &lt;&lt; "Device context failed" &lt;&lt; std::endl; std::cout &lt;&lt; std::endl &lt;&lt; "Press ENTER to exit" &lt;&lt; std::endl; std::getline(std::cin, trash); return 1; } // pixel format pf = ChoosePixelFormat(hDC, &amp;pfd); res = SetPixelFormat(hDC, pf, &amp;pfd); if (!(hRC = wglCreateContext(hDC))) { std::cout &lt;&lt; "Render context failed" &lt;&lt; std::endl; std::cout &lt;&lt; std::endl &lt;&lt; "Press ENTER to exit" &lt;&lt; std::endl; std::getline(std::cin, trash); return 1; } if(!wglMakeCurrent(hDC,hRC)) { // fail: wglMakeCurrent returns 0 std::cout &lt;&lt; "Activating render context failed" &lt;&lt; std::endl; std::cout &lt;&lt; std::endl &lt;&lt; "Press ENTER to exit" &lt;&lt; std::endl; std::getline(std::cin, trash); return 1; } std::cout &lt;&lt; "OpenGL 1.2 support ... "; if (GLEE_VERSION_1_2) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl; } std::cout &lt;&lt; "OpenGL 1.3 support ... "; if (GLEE_VERSION_1_3) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl; } std::cout &lt;&lt; "OpenGL 1.4 support ... "; if (GLEE_VERSION_1_4) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl; } std::cout &lt;&lt; "OpenGL 1.5 support ... "; if (GLEE_VERSION_1_5) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl; } std::cout &lt;&lt; "OpenGL 2.0 support ... "; if (GLEE_VERSION_2_0) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl; } std::cout &lt;&lt; "OpenGL 2.1 support ... "; if (GLEE_VERSION_2_1) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl; } std::cout &lt;&lt; "OpenGL 3.0 support ... "; if (GLEE_VERSION_3_0) { std::cout &lt;&lt; "OK" &lt;&lt; std::endl; } else { std::cout &lt;&lt; "FAIL" &lt;&lt; std::endl; } std::cout &lt;&lt; std::endl &lt;&lt; "Press ENTER to exit" &lt;&lt; std::endl; std::getline(std::cin, trash); // cleanup wglMakeCurrent(NULL, NULL); /* make our context not current */ wglDeleteContext(hRC); /* delete the rendering context */ ReleaseDC(hWnd, hDC); /* release handle to DC */ return 0; </code></pre> <p>}</p> <hr> <p><strong>edit:</strong> I know that wglMakeCurrent() fails if either of the handles passed to it is invalid or if the rendering context that is to become current is presently current for another thread, but I am not really sure which of these is true in this case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload