我在尝试在Visual Studio 2015下调试我的64位应用程序时遇到问题。当我将其切换到 Debug模式时,它在创建窗口时崩溃。在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 *”,我没有得到那部分,甚至不确定彼此之间应该如何工作转换,似乎很麻烦。有想法吗?
最佳答案
您输入错误: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),因此您传入的这两个指针被截断了。
关于c++ - 在x64平台下,wglCreateContextAttribsARB在 Debug模式下崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34693808/