# OpenGL ChoosePixelFormat() gives an "unhandled exception"

## Recommended Posts

Hi. Something strange happened. Up until (and including) last night it seemed my code worked great. When I started up visual studio today and tried to run my game, I got an unhandled exception error message. I started digging, and found the line that gave me the error. It was the call to the function ChoosePixelFormat in my opengl initialization code. Here's my call to the ChoosePixelFormat function: p_nPixelFormatID = ChoosePixelFormat(m_hDC, &p_pfPixelFormat); And here's what I do know: - m_hDC is a valid HDC (I have tested it by calling TextOut to write to it, and the correct text appears in the correct position). - It worked yesterday, but not today even though I didn't make any changes in my code. - I've tried calling ChoosePixelFormat right after I create my window, in the very beginning of the program to see if there's some bad code that messes things up (the answer was negative - I still got the same error). - The window handle that I passed to the GetDC() function to get the HDC is valid. - Here's the full error message: Unhandled exception at 0x69736a3d in Game.exe: 0xC0000005: Access violation reading location 0x000000a3. I did however install Windows Vista here the other day, and although the code did run on Vista yesterday, might there be a compatibility problem between Vista and Visual studio 2005 that is causing it? Also I'm wondering: Is ChoosePixelFormat a part of the Windows OpenGL library, or is it a Win32 API function? If it's the first one maybe I should try finding some other OpenGL library? Currently I'm using the OpenGL headers and .lib files included in VS2005. If I don't find any solution to this problem I'll probably try setting up OpenGL with SDL instead.. Edit: I forgot some information that might be useful. If I pass "NULL" to the ChoosePixelFormat function instead of m_hDC, I get no error message. Also if I create a new HDC with CreateDC(), I also get no error message... /ponders Thanks in advance for any help on this very annoying matter.

##### Share on other sites
Sup man,

Hmmm... It's most likely has something to do w/OpenGL and Vista compatiblity issues. As for the ChoosePixelFormat function, it is part of the old Win32 GDI functionality ( which might explain those issues ). Did you change any fields in the PIXELFORMATDESCRIPTOR instance? If not, maybe it'd be worth changing some of them around ( particually the iLayerType & dwFlags members ) to see if it changes anything...

Peace,

ahayweh

##### Share on other sites
Quote:
 Original post by ahaywehSup man, Hmmm... It's most likely has something to do w/OpenGL and Vista compatiblity issues. As for the ChoosePixelFormat function, it is part of the old Win32 GDI functionality ( which might explain those issues ). Did you change any fields in the PIXELFORMATDESCRIPTOR instance? If not, maybe it'd be worth changing some of them around ( particually the iLayerType & dwFlags members ) to see if it changes anything...Peace, ahayweh

I didn't change anything at all in the initialization code. After I read your post I did try changing the dwFlags member. I removed one by one flag, tried different combinations, tried no flags at all but I get the same error. According to the Visual Studio documentation on the PIXELFORMATDESCRIPTOR structure, iLayerType is ignored:
"iLayerType
Ignored. Earlier implementations of OpenGL used this member, but it is no longer used."

I tried setting it to 0 to see if it changed anything (it was set to PFD_MAIN_PLANE earlier), but unfortunately I got the same results :(

Thanks for trying.. :)

##### Share on other sites
This is getting plain silly.

I now tried to set up SDL to try using that instead, and the function "SDL_SetVideoMode" gives me the EXACT same error. I'm guessing that function calls ChoosePixelFormat, and it fails... /sigh

/considers reinstalling WinXP.

Edit: And there I found the error. It's my graphics drivers... I used the whole yesterday to find working drivers for my Geforce 7800GT card (all the new ones wouldn't let me run on higher resolutions than 1024x768), so I found some older drivers. Obviously they didn't work as well as they should...

Windows XP here I come! :P

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627704
• Total Posts
2978715
• ### Similar Content

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• 21
• 14
• 12
• 10
• 12