wglMakeCurrent used pre main causes blank screen?

Hello, I’m using OpenGL with C++ code.
I made a class for the OpenGL context, and all renders correctly when I use it.

However if I try to define a context as a global variable (initialized before main is reached), I get blank window but no error (via glGetError or GetLastError).

This is the constructor:

context::context( HDC hDC ){
    m_handle = wglCreateContext( hDC );
    //error checking here

    wglMakeCurrent( hDC, m_handle )
    //more error checking here

    //set gl function pointers with wglGetProcAddress here (requires context to be current)

Changing the constructor to this solves the problem for using a global context:

context::context( HDC hDC ){
    m_handle = wglCreateContext( hDC );
    //error checking here

This is unsatisfying, as the function pointers need to be initialized later, and if any calls happens in between I would get memory violation.

I checked if the global context was set correctly in the first case, by calling wglGetCurrentContext and wglGetCurrentDC after wglMakeCurrent, and the values I got matched the handles of my context and my DC.

Also disabling the current context, and reenabling it after main has been reached still wouldn’t change the problem. Yet wglMakeCurrent sets the handles correctly still.

I can also use a context that has been created after main locally, even if the global context is not functionning. This suggests me that wglMakeCurrent causes an unreported problem if used in a static initialization.

I would like to know if there is a way to make it function, or report this problem somehow.
Thanks in advance.

Don’t do that. It never works. You should never attempt to do anything graphics/window related before main. Indeed, you should avoid doing any significant work before main.

Also, for your specific case, where do you get your HDC from? Where does the window come from which provides that HDC?

Again, this is a problem easily solved by not having significant code execute before main.

Thank you for your quick answer.

I was trying to see to which extent the instance of my class was functionning or signaling an error if it didn’t.

In the case I try for a global context, I get the HDC from a global window as well, that I create just above to avoid an initialization fiasco.
Everything works even like that though, providing I use the second version of the constructor, and initialize the function pointers later in main.

In fact the issue was related to a resolution change.

In the case the context is created between window creation and window resolution setting, and if no resolution change occurs during execution to change any state (so no call to glViewport for instance), the screen is blank… because the context considers the resolution to be 0,0 from start.

Now everything works including in the tests I described.