wglMakeCurrent在x64上失败

时间:2013-10-07 22:38:16

标签: c++ winapi opengl 64-bit wgl

我正在尝试在x64平台上创建一个OpenGL窗口。 我的初始化代码适用于x86 / Win32,但是对于“wglMakeCurrent”的x64失败了。我想问题是在pixelformat的设置或获取DC(getDC())。我已经为我的PIXELFORMATDESCRIPTOR尝试了不同的设置(假设OpenGL的x64实现不支持我正在使用的那个),但在那里没有成功。调试器指示hdc可能已损坏 - (类似于0xfffffffff10102e0) - 但是wglCreateContext然后返回一个有效的外观hglrc(即0x0000000000010000)。但即使我将hdc中的值动态更改为0x0000000010102e0(使用调试器,在调用wglCreateContext之前)wglMakeCurrent仍然失败。

我在使用Visual Studio 12 RC 1的Windows 8上。 我在这里做错了什么想法?或者是否有一些x64 OpenGL实现的限制?

#include <windows.h>
#pragma comment(lib, "OpenGL32.lib")

static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
  switch (uMsg){
  case WM_DESTROY:
    PostQuitMessage(0);
    break;
  default:
    break;
  }
  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
  WNDCLASSEX wc;
  ZeroMemory(&wc, sizeof(WNDCLASSEX));
  wc.cbSize = sizeof(WNDCLASSEX);
  wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  wc.lpfnWndProc = WindowProc;
  wc.hInstance = nullptr;
  wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  wc.lpszClassName = "WindowClassTest";
  RegisterClassEx(&wc);
  DWORD dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
  RECT WindowRect;
  WindowRect.left = (long)0;
  WindowRect.right = (long)640;
  WindowRect.top = (long)0;
  WindowRect.bottom = (long)480;
  AdjustWindowRect(&WindowRect, dwStyle, FALSE);
  HWND hWnd = CreateWindowEx(0,
    "WindowClassTest",
    "WindowTitle",
    dwStyle,
    0, 0,
    WindowRect.right - WindowRect.left,
    WindowRect.bottom - WindowRect.top,
    nullptr,
    nullptr,
    wc.hInstance,
    (LPVOID) nullptr);
  wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  ShowWindow(hWnd, SW_SHOW);
  SetFocus(hWnd);
  HDC hdc = GetDC(hWnd);
  int         pf;
  PIXELFORMATDESCRIPTOR pfd;
  memset(&pfd, 0, sizeof(pfd));
  pfd.nSize = sizeof(pfd);
  pfd.nVersion = 1;
  pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
  pfd.iPixelType = PFD_TYPE_RGBA;
  pfd.cColorBits = 32;
  pf = ChoosePixelFormat(hdc, &pfd);
  if (pf == 0) {
    MessageBox(NULL, "ChoosePixelFormat() failed:  "
      "Cannot find a suitable pixel format.", "Error", MB_OK);
  }
  if (SetPixelFormat(hdc, pf, &pfd) == FALSE) {
    MessageBox(NULL, "SetPixelFormat() failed:  "
      "Cannot set format specified.", "Error", MB_OK);
  }
  DescribePixelFormat(hdc, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  HGLRC hglrc = wglCreateContext(hdc);
  if (!wglMakeCurrent(hdc, hglrc)){
    MessageBox(NULL, "wglMakeCurrent() failed:  "
      "Cannot make context current.", "Error", MB_OK);
  }

  /* left out other unnecessary code here*/
  return 0;
}

1 个答案:

答案 0 :(得分:4)

在某些情况下,在Windows 8.x虚拟机中运行64位OpenGL应用程序时,这似乎是一个非常具体的错误。根据可用的报告(见下文),它发生在

  • 使用Visual Studio&gt; = 2012
  • 为x64编译了应用程序
  • 在VM 8.x(Parallels / VMWare)中运行的Windows 8.x中执行

我在用于编译Windows应用程序的Parallels VM中遇到了同样的问题。一个临时的解决方法是使用32位构建或使用以前版本的Visual Studio(我使用2010)编译为64位。

重要的是,在非模拟硬件上运行的Windows 8.x 似乎受到影响。

与提及此问题的其他人的链接: