在x64平台

时间:2016-01-09 13:06:44

标签: c++ opengl glew

我在Visual Studio 2015下尝试调试我的64位应用程序时遇到了一个问题。当我将其切换到调试模式时,它会在窗口创建期间崩溃。这种情况在32位模式下没有发生;

这是编辑窗口初始化代码:

int indexPF;
WNDCLASS WinClass;

WinClass.style = CS_OWNDC | CS_PARENTDC;
WinClass.lpfnWndProc = WndProc;
WinClass.cbClsExtra = 0;
WinClass.cbWndExtra = 0;
WinClass.hInstance = hInstance;
WinClass.hIcon = LoadIcon(NULL, IDC_ICON);
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WinClass.hbrBackground = (HBRUSH)GetStockObject(5);
WinClass.lpszMenuName = NULL;
WinClass.lpszClassName = "N2";

if (!RegisterClass(&WinClass))
{
    ...report error and terminate...
}

Logger::Inst() << " ~RegisterClass;" << endl;

//CREATE WINDOW

if (fullScreen)
{
    if ((hwnd = CreateWindowEx(WS_EX_LEFT, "N2", "N2",
        WS_POPUP,
        0, 0, width, height,
        NULL, NULL, hInstance, NULL)) == 0)
    {
        ...report error and terminate...
    }
}
else
{
    if ((hwnd = CreateWindowEx(WS_EX_LEFT, "N2", "N2",
        WS_OVERLAPPEDWINDOW,
        0, 0, width, height,
        NULL, NULL, hInstance, NULL)) == 0)
    {
        ...report error and terminate...
    }
}

Logger::Inst() << " ~CreateWindow;" << endl;

//PFD SETUP
PIXELFORMATDESCRIPTOR pfd =
{
    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,
    24,
    8, 0, PFD_MAIN_PLANE, 0, 0, 0x00FF00FF, 0
};

//HDC
if ((hdc = GetDC(hwnd)) == NULL)
{
    ...report error and terminate...
}

Logger::Inst() << " ~GotHDC;" << endl;

//SET PIXEL FORMAT
indexPF = ChoosePixelFormat(hdc, &pfd);
if (!indexPF)
{
    ...report error and terminate...
}

if (!SetPixelFormat(hdc, indexPF, &pfd))
{
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_SWAP_EXCHANGE;
    indexPF = ChoosePixelFormat(hdc, &pfd);

    if (!SetPixelFormat(hdc, indexPF, &pfd))
    {
        ...report error and terminate...
    }
}

Logger::Inst() << " ~SetPFD;" << endl;

//TEMP CONTEXT TO ACQUIRE POINTER
HGLRC tempContext = wglCreateContext(hdc);

if (!tempContext) {
    ...report error and terminate...
}

if (!wglMakeCurrent(hdc, tempContext)) {
    ...report error and terminate...
}

int major, minor; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor);
if (major < 4 || minor < 1) {
    ...report error and terminate...
}

const int attribs[] =
{
    WGL_CONTEXT_MAJOR_VERSION_ARB, major,
    WGL_CONTEXT_MINOR_VERSION_ARB, minor,
    WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
    0, 0
};

PFNWGLCREATEBUFFERREGIONARBPROC wglCreateContextAttribsARB = (PFNWGLCREATEBUFFERREGIONARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
if (!wglCreateContextAttribsARB) {
    ...report error and terminate...
}

**!!! CRASH HERE !!!**
if (!(hglrc = (HGLRC)wglCreateContextAttribsARB(hdc, 0, (UINT)attribs))) {
    ...report error and terminate...
}

调试器显示它恰好发生在wglCreateContextAttribsARB(nvoglv64.dll!0000000074ccbdfa)。这对我来说是一个完全的谜。我唯一的线索是,在切换到x64后,它需要传递为“UINT”而不是“const int *”的属性,我没有得到那个部分,甚至不确定彼此之间的转换应该如何工作,看起来很可疑。想法?

1 个答案:

答案 0 :(得分:4)

你有一个拼写错误:wglCreateContextAttribsARB的类型错了。它应该是PFNWGLCREATECONTEXTATTRIBSARBPROC

当你瞄准32位时它起作用的原因是因为两者的签名在32位中基本相同:

wglCreateContextAttribsARB:

typedef HGLRC (WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int *attribList);

wglCreateBufferRegionARB:

typedef HANDLE (WINAPI * PFNWGLCREATEBUFFERREGIONARBPROC) (HDC hDC, int iLayerPlane, UINT uType);

wglCreateContextAttribsARB的第二个和第三个参数是指针(HGLRC是句柄,它只是一个指针),wglCreateBufferRegionARB的第二个和第三个参数是整数。在32位中,指针的大小为32位,因此签名基本相同。

在64位中,指针的大小为64位,但整数仍为32位(假设您使用的是MSVC),因此传入的两个指针被截断。