ChoosePixelFormat访问冲突读取位置

时间:2017-02-20 09:35:30

标签: c++ windows opengl

我正在尝试创建一个简单的OpenGL上下文,但程序在ChoosePixelFormat上崩溃,错误为“OpenGL.exe中0x779CE0E6(ntdll.dll)处的未处理异常:0xC0000005:访问冲突读取位置0x00000044。”这段相同的代码曾经用过一段时间,但由于某种原因不再适用。我尝试更新我的图形驱动程序无济于事。如果重要的话,我有64位Windows 7 Home premium,GeForce 570和Intel Core i7-2600 3,40 GHz。

这是我按照执行顺序使用的代码,它崩溃了:

GLEngine gl(WndProc); //WndProc just calls DefWindowProc
- calls ->
GLEngine::GLEngine(WNDPROC wndproc) { //Initialize class

    hRC = NULL;
    hDC = NULL;
    hWnd = NULL;
    fullscreen = false;
    active = false;

    proc = wndproc;
    itemsLength = 0;

    currentActive = this;

    success = true;
}

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR    lpCmdLine, _In_ int       nCmdShow) {

    gl.CreateGL2Window("Test", 1300, 900, 8, false, true);

- calls ->

bool GLEngine::CreateGL2Window(char* title, int width, int height, bool internalflag) {

    GLuint pixelFormat;
    WNDCLASSEX wc;

    DWORD dwExStyle;
    DWORD dwStyle;

    RECT windowRect;
    windowRect.left = (long)0;
    windowRect.right = (long)width;
    windowRect.top = (long)0;
    windowRect.bottom = (long)height;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance; //hInstance is NULL here, should it be something else?
    wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = _T("OpenGL");
    wc.hIconSm = NULL;

    if(!RegisterClassEx(&wc)) {

        MessageBox(NULL, _T("Failed To Register The Window Class."), _T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
        return false;
    }
    hInstance = wc.hInstance; //hInstance isn't NULL anymore

    dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    dwStyle = WS_OVERLAPPEDWINDOW;

    AdjustWindowRectEx(&windowRect, dwStyle, false, dwExStyle);

    if(!(hWnd = CreateWindowEx(dwExStyle, _T("OpenGL"), 
        (wchar_t*)title, 
        WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
        0, 0,
        windowRect.right - windowRect.left,
        windowRect.bottom - windowRect.top,
        NULL,
        NULL,
        hInstance,
        NULL))) {
            DestroyGLWindow();
            MessageBox(NULL, _T("Window Creation Error."), _T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
            return false;
    }

    if(!(hDC = GetDC(hWnd))) {

        DestroyGLWindow();
        MessageBox(NULL, _T("Can't Create A GL Device Context."), _T("ERROR"), MB_OK | MB_ICONEXCLAMATION);
        return false;
    }

    PIXELFORMATDESCRIPTOR pfd2 = { sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        32,
        0, 0, 0, 0, 0, 0,
        0,
        0,
        0,
        0, 0, 0, 0,
        32,
        8,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0
    };
    if(!(pixelFormat = ChoosePixelFormat(hDC, &pfd2)) {

GLEngine是一个dll。这应该没关系,但信息不会受到伤害。

1 个答案:

答案 0 :(得分:0)

您是否使用GetProcAddress获取了函数的地址?有故障的司机也可能导致崩溃。 ChoosePixelFormat可以在驱动程序端实现和导出,但可能不会导出,因此应该使用系统过程。导出一个可能会或可能不会填充描述符结构,具体取决于实现,因此您需要调用DescribePixelFormat。