KiraBox
KiraBox

Reputation: 79

wglMakeCurrent fails with 1282

I am creating a visual studio Win32 application using OpenGL. However when I call glewInit and then GetLastError(), I get error 127 ("The specified procedure could not be found.") I am statically linking to opengl as follows:

#define GLEW_STATIC
#include <glew.h>
#include <wglew.h>
#include <gl/gl.h>
#include <gl/glu.h>

// link with libraries
#pragma comment(lib, "openGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glew32s.lib")

My guess is that for some reason at run-time my application can't find the function but I'm not sure why and it's driving me nuts. I've tried placing the libraries directly in the executable directory but have had no luck. Here is the offending code:

      // create a device context
  m_deviceContext = GetDC(m_windowHandle);

  // choose a pixel format
  PIXELFORMATDESCRIPTOR pfd;
  memset(&pfd, 0, sizeof(pfd));

  pfd.nSize = sizeof(pfd);
  pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; // double-buffering, opengl, and render to window
  pfd.iPixelType = PFD_TYPE_RGBA;
  pfd.cColorBits = 32; // 32 bits of color information (the higher, the more colors)
  pfd.cDepthBits = 32; // 32 bits of color information (the higher, the more depth levels)
  pfd.iLayerType = PFD_MAIN_PLANE;

  int pixelFormat = ChoosePixelFormat(m_deviceContext, &pfd);
  assert(pixelFormat);

  // set the pixel format
  BOOL res = SetPixelFormat(m_deviceContext, pixelFormat, &pfd);
  assert(res);

  // this creates an openGL 2.1 context for the device context
  HGLRC tempContext = wglCreateContext(m_deviceContext);
  wglMakeCurrent(m_deviceContext, tempContext);

  // enable GLEW so we can try to create a 3.2 context instead
  DWORD error = GetLastError();
  GLenum err = glewInit();
  error = GetLastError();
  assert(err == GLEW_OK);

Some specifics about my setup. I altered my Working Directory to be where the executable is generated (($ProjectDir)\Binaries\Win32). Before this things worked fine. I tried adding additional dependencies and altering the additional library directories with no luck.

What am I missing for my program to find the glewInit procedure? I appreciate any help or suggestions.

EDIT: glGetError() returns 1282 (INVALID_OPERATION) after making this call to wglMakeCurrent():

  // attempt to create the opengl 3.2 context
  int attr[] = {
    WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
    WGL_CONTEXT_MINOR_VERSION_ARB, 2,
    WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
    0
  };

  if(wglewIsSupported("WGL_ARB_create_context") == 1)
  {
    CheckOpenGLError();
    m_renderContext = wglCreateContextAttribsARB(m_deviceContext, NULL, attr);
    CheckOpenGLError();
    wglMakeCurrent(NULL, NULL);
    CheckOpenGLError();   // <-- This guy calls glGetError() and returns 1282
    wglDeleteContext(tempContext);
    CheckOpenGLError();
    wglMakeCurrent(m_deviceContext, m_renderContext);
  }

What I thought was the problem was not. It looks like instead my problem is that this call to wglMakeCurrent is failing, and I am unable to render to my window. After doing some research, the documentation here suggests that it is perfectly valid to call with NULL as the HDC because it is ignored, and the rendering context should be made not current.

New Question: What reasons might there be for wglMakeCurrent(NULL, NULL) to fail? I've tried wglMakeCurrent(m_deviceContext, NULL) as well just in case, but it failed as well.

Upvotes: 0

Views: 2191

Answers (1)

datenwolf
datenwolf

Reputation: 162184

I am creating a visual studio Win32 application using OpenGL. However when I call glewInit and then GetLastError(), I get error 127 ("The specified procedure could not be found.")

It's been a long known issue that GLEW may leave OpenGL in a errornous state after initialization. Ignore it. The idiomatic way of initializing GLEW is

if( GLEW_OK != glewInit() ) {
    handle_glew_init_error_condition();
}
else {
    /* flush the OpenGL error state, ignoring all errors */
    while( GL_NO_ERROR != glGetError() );
}

Note that you must call glGetError in a loop, because it's allowed to have multiple error conditions be set and glGetError reports and clears them only one after another.

I am statically linking to opengl as follows:

Certainly not. .lib files can also be used for dynamic linking; in that case they contain the name of the DLL and the table of entry points.


Update regarding wglMakeCurrent(NULL, NULL)

Once you unbound the OpenGL context, what error state is glGetError supposed to report from? There's no OpenGL context it could work with, so it's reporting you that. glGetError itself is performing an invalid operation, because you call it without a OpenGL context being active.

Upvotes: 3

Related Questions