PDA

View Full Version : wglMakeCurrent - The handle is invalid



scarecr0w132
12-29-2013, 06:50 PM
Hello,


I am fairly new to OpenGL, I don't fully understand everything yet:)
I have a viewer with a context. It is being drawn on the UI which has another separate context. (I am using QT for UI, and trying to draw widgets ontop of viewer.)
wglMakeCurrent() which gets called in the viewer code, fails and says from GetLastError(): wglMakeCurrent() failed. The handle is invalid.
This is how it is called:


...
if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
...

I uploaded the project here if anyone wants to take a look www.uppit.com/tk1gzrwu0hz5/project.zip
What is a likely cause of this?

l_belev
01-21-2014, 03:10 AM
why don't you assume the error you get is correct and don't you check the handles? first find which of the 2 is invalid - try to do something else with them.
for the hdc handle, you can try to call the func GetDeviceCaps. if that fail then tha hdc is the problem. also you can try "WindowFromDC()" - in windows the HDC must belong
to some window to be usable for opengl. you cant use HDC you got from CreateDC (well at least not that easily).
for the hglrc see for example if you call wglDeleteContext instead of wglMakeCurrent, will it fail too. Sorry, I can't debug your project because my work is more than enough.
Generally learn to debug your own code, especially for elementary problems like this, instead of asking other people to do it for you.

Other things off the top of my head: the hdc must have set pixel format and it must be the same as the one of the hglrc. for the hdc you can use GetPixelFormat().
For the hglrc, i don't remember a function to ask for this. You may have to check where/how you create it.
I assumed you checked that none of the handles is NULL, did you?

scarecr0w132
01-21-2014, 03:33 PM
why don't you assume the error you get is correct and don't you check the handles? first find which of the 2 is invalid - try to do something else with them.
for the hdc handle, you can try to call the func GetDeviceCaps. if that fail then tha hdc is the problem. also you can try "WindowFromDC()" - in windows the HDC must belong
to some window to be usable for opengl. you cant use HDC you got from CreateDC (well at least not that easily).
for the hglrc see for example if you call wglDeleteContext instead of wglMakeCurrent, will it fail too. Sorry, I can't debug your project because my work is more than enough.
Generally learn to debug your own code, especially for elementary problems like this, instead of asking other people to do it for you.

Other things off the top of my head: the hdc must have set pixel format and it must be the same as the one of the hglrc. for the hdc you can use GetPixelFormat().
For the hglrc, i don't remember a function to ask for this. You may have to check where/how you create it.
I assumed you checked that none of the handles is NULL, did you?

Hello I_belev,

Thank you for replying.
I will look into everything you said.

thanks