OpenGL from DLL causing errors

I have following scenario:

my GUI is in C#, and it grabs images from an attached camera. The application then pipes the image data pointer to the DLL, which should display the image.

The point is, I call OpenGLDisplay_Init(), which results in the error returned from glGetError() as 0. The initialization itself seems to work fine, and the image on my second monitor goes black.
My main application is not using OpenGL in any way. The interesting thing is that in

void OpenGL_Display::DrawNewImage(int width, int height, unsigned char* imageData, bool flip)
{
GLenum mError = glGetError();

BOOL returnVal = wglMakeCurrent(hDC, hRC);
mError = glGetError();

UpdateGLBuffers(width, height, imageData);    
DrawGLScene(flip);
SwapBuffers(hDC);

}

the first glGetError() already returns 1282, so the OpenGL Context seems to have been invalidated, but I don’t know how, and if it did, why I cannot get it back via wglMakeCurrent().
Can someone help me figure out what’s going wrong there?

Edit: GetLastError() returns 170, saying that the requested resource is in use. Trying to find a way around that. wglGetCurrentDC / wglGetCurrentContext both return NULL, and I cannot do anything so far to “unbusy” those resources.

.cpp:

/** Based on code by NeHe tutorials
    Adapted to display live camera display with OpenGL
*/


#include "OpenGL-Display.h"


using namespace OGD;


OpenGL_Display::OpenGL_Display()
{


}


OpenGL_Display::~OpenGL_Display()
{
    KillGLWindow();
}


void OpenGL_Display::Init_Display(int width, int height, int posX, int posY)
{
    if (!CreateGLWindow("Ophthametrics Live Display", width, height, 24, false, posX, posY))
    {
        throw;
    }    
}


LRESULT CALLBACK OpenGL_Display::WndProc(    HWND    hWnd,            // Handle For This Window
                            UINT    uMsg,            // Message For This Window
                            WPARAM    wParam,            // Additional Message Information
                            LPARAM    lParam)            // Additional Message Information
{
    switch (uMsg)                                    // Check For Windows Messages
    {
        case WM_SYSCOMMAND:                            // Intercept System Commands
        {
            switch (wParam)                            // Check System Calls
            {
                case SC_SCREENSAVE:                    // Screensaver Trying To Start?
                case SC_MONITORPOWER:                // Monitor Trying To Enter Powersave?
                return 0;                            // Prevent From Happening
            }
            break;                                    // Exit
        }


        case WM_CLOSE:                                // Did We Receive A Close Message?
        {
            PostQuitMessage(0);                        // Send A Quit Message
            return 0;                                // Jump Back
        }


        case WM_SIZE:                                // Resize The OpenGL Window
        {
            oglDisp.ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
            return 0;                                // Jump Back
        }
    }


    // Pass All Unhandled Messages To DefWindowProc
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}




bool OpenGL_Display::CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag, int posX, int posY)
{
    GLuint        PixelFormat;            // Holds The Results After Searching For A Match
    WNDCLASS    wc;                        // Windows Class Structure
    DWORD        dwExStyle;                // Window Extended Style
    DWORD        dwStyle;                // Window Style
    RECT        WindowRect;                // Grabs Rectangle Upper Left / Lower Right Values
    WindowRect.left=(long)0;            // Set Left Value To 0
    WindowRect.right=(long)width;        // Set Right Value To Requested Width
    WindowRect.top=(long)0;                // Set Top Value To 0
    WindowRect.bottom=(long)height;        // Set Bottom Value To Requested Height


    hInstance            = GetModuleHandle(NULL);                // Grab An Instance For Our Window
    wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;    // Redraw On Size, And Own DC For Window.
    wc.lpfnWndProc        = WndProc;                                // WndProc Handles Messages
    wc.cbClsExtra        = 0;                                    // No Extra Window Data
    wc.cbWndExtra        = 0;                                    // No Extra Window Data
    wc.hInstance        = hInstance;                            // Set The Instance
    wc.hIcon            = LoadIcon(NULL, IDI_WINLOGO);            // Load The Default Icon
    wc.hCursor            = LoadCursor(NULL, IDC_ARROW);            // Load The Arrow Pointer
    wc.hbrBackground    = NULL;                                    // No Background Required For GL
    wc.lpszMenuName        = NULL;                                    // We Don't Want A Menu
    wc.lpszClassName    = "OpenGL_Display";                                // Set The Class Name


    int error = 0;
    if ((error=RegisterClass(&wc))==0)                                    // Attempt To Register The Window Class
    {
        char* message = new char[256];
        sprintf_s(message, 256, "Failed To Register The Window Class. Error: %d", error);
        MessageBox(NULL, message,"ERROR",MB_OK|MB_ICONEXCLAMATION);
        delete message;
        return FALSE;                                            // Return FALSE
    }


    
    
    dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;            // Window Extended Style
    dwStyle=WS_VISIBLE | WS_POPUP |    WS_CLIPSIBLINGS | WS_CLIPCHILDREN;;                            // Windows Style
    
    AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);        // Adjust Window To True Requested Size
    //HWND hwndC = GetConsoleWindow();
    // Create The Window
    if (!(hWnd=CreateWindowEx(    dwExStyle,                            // Extended Style For The Window
                                "OpenGL_Display",                    // Class Name
                                title,                                // Window Title
                                dwStyle |                            // Defined Window Style
                                WS_CLIPSIBLINGS |                    // Required Window Style
                                WS_CLIPCHILDREN,                    // Required Window Style
                                posX, posY,                            // Window Position
                                WindowRect.right-WindowRect.left,    // Calculate Window Width
                                WindowRect.bottom-WindowRect.top,    // Calculate Window Height
                                NULL,                                // No Parent Window
                                NULL,                                // No Menu
                                hInstance,                            // Instance
                                NULL)))                                // Dont Pass Anything To WM_CREATE
    {
        KillGLWindow();                                // Reset The Display
        MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;                                // Return FALSE
    }
    
    static    PIXELFORMATDESCRIPTOR pfd=                // pfd Tells Windows How We Want Things To Be
    {
        sizeof(PIXELFORMATDESCRIPTOR),                // Size Of This Pixel Format Descriptor
        1,                                            // Version Number
        PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
        PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
        PFD_DOUBLEBUFFER,                            // Must Support Double Buffering
        PFD_TYPE_RGBA,                                // Request An RGBA Format
        bits,                                        // Select Our Color Depth
        0, 0, 0, 0, 0, 0,                            // Color Bits Ignored
        0,                                            // No Alpha Buffer
        0,                                            // Shift Bit Ignored
        0,                                            // No Accumulation Buffer
        0, 0, 0, 0,                                    // Accumulation Bits Ignored
        16,                                            // 16Bit Z-Buffer (Depth Buffer)  
        0,                                            // No Stencil Buffer
        0,                                            // No Auxiliary Buffer
        PFD_MAIN_PLANE,                                // Main Drawing Layer
        0,                                            // Reserved
        0, 0, 0                                        // Layer Masks Ignored
    };
    
    if (!(hDC=GetDC(hWnd)))                            // Did We Get A Device Context?
    {
        KillGLWindow();                                // Reset The Display
        MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;                                // Return FALSE
    }


    if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))    // Did Windows Find A Matching Pixel Format?
    {
        KillGLWindow();                                // Reset The Display
        MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;                                // Return FALSE
    }


    if(!SetPixelFormat(hDC,PixelFormat,&pfd))        // Are We Able To Set The Pixel Format?
    {
        KillGLWindow();                                // Reset The Display
        MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;                                // Return FALSE
    }


    if (!(hRC=wglCreateContext(hDC)))                // Are We Able To Get A Rendering Context?
    {
        KillGLWindow();                                // Reset The Display
        MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;                                // Return FALSE
    }


    if(!wglMakeCurrent(hDC,hRC))                    // Try To Activate The Rendering Context
    {
        KillGLWindow();                                // Reset The Display
        MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;                                // Return FALSE
    }


    ShowWindow(hWnd,SW_SHOW);                        // Show The Window
    SetForegroundWindow(hWnd);                        // Slightly Higher Priority
    SetFocus(hWnd);                                    // Sets Keyboard Focus To The Window
    ReSizeGLScene(width, height);                    // Set Up Our Perspective GL Screen


    if (!InitGL())                                    // Initialize Our Newly Created GL Window
    {
        KillGLWindow();                                // Reset The Display
        MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
        return FALSE;                                // Return FALSE
    }


    return TRUE;                                    // Success
}




int OpenGL_Display::LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
{
    // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit


    glGenTextures(1, &texture);                    // Create The Texture
    // Typical Texture Generation Using Data From The Bitmap
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Minimizing filter, in case display is smaller image size
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Magnifying filter, in case display is smaller image size
    
    return 1;                                        // Return The Status
}


GLvoid OpenGL_Display::ReSizeGLScene(GLsizei width, GLsizei height)        // Resize And Initialize The GL Window
{
    if (height==0)                                        // Prevent A Divide By Zero
    {
        height=1;                                        // Making Height Equal One
    }


    glViewport(0,0,width,height);                        // Reset The Current Viewport


    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                    // Reset The Projection Matrix


    // Calculate The Aspect Ratio Of The Window
    gluPerspective(25.0f,1.0f,0.1f,100.0f);


    glMatrixMode(GL_MODELVIEW);                            // Select The Modelview Matrix
    glLoadIdentity();                                    // Reset The Modelview Matrix
}


int OpenGL_Display::InitGL(GLvoid)                                        // All Setup For OpenGL Goes Here
{
    if (!LoadGLTextures())                                // Jump To Texture Loading Routine
    {
        return FALSE;                                    // If Texture Didn't Load Return FALSE
    }


    glEnable(GL_TEXTURE_2D);                            // Enable Texture Mapping
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                // Black Background
    glClearDepth(1.0f);                                    // Depth Buffer Setup
    glDisable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                                // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);    // Really Nice Perspective Calculations
    return TRUE;                                        // Initialization Went OK
}


void OpenGL_Display::UpdateGLBuffers(int width, int height, unsigned char* imageData) 
{
    GLenum mError = glGetError();
    glBindTexture(GL_TEXTURE_2D, texture);
    mError = glGetError();
    glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, imageData);
    mError = glGetError();
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    mError = glGetError();
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    mError = glGetError();
}


int OpenGL_Display::DrawGLScene(bool flip)                                    // Here's Where We Do All The Drawing
{
    GLenum mError = glGetError();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // Clear The Screen And The Depth Buffer
    glLoadIdentity();                                    // Reset The View
    glTranslatef(0.0f,0.0f,-5.0f);
    mError = glGetError();
    glBindTexture(GL_TEXTURE_2D, texture);
    mError = glGetError();
    glColor4f(1.0, 1.0, 1.0, 1.0);
    mError = glGetError();
    if(flip)
    {
        glBegin(GL_QUADS);
            // Front Face
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  0.5f);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  0.5f);
        glEnd();
        mError = glGetError();
    }
    else
    {
        glBegin(GL_QUADS);
            // Front Face
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  0.5f);
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  0.5f);
        glEnd();
        mError = glGetError();
    }
    mError = glGetError();    
    return TRUE;                                        // Keep Going
}


GLvoid OpenGL_Display::KillGLWindow(GLvoid)                                // Properly Kill The Window
{
    if (fullscreen)                                        // Are We In Fullscreen Mode?
    {
        ChangeDisplaySettings(NULL,0);                    // If So Switch Back To The Desktop
        ShowCursor(TRUE);                                // Show Mouse Pointer
    }


    if (hRC)                                            // Do We Have A Rendering Context?
    {
        if (!wglMakeCurrent(NULL,NULL))                    // Are We Able To Release The DC And RC Contexts?
        {
            MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
        }


        if (!wglDeleteContext(hRC))                        // Are We Able To Delete The RC?
        {
            MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
        }
        hRC=NULL;                                        // Set RC To NULL
    }


    if (hDC && !ReleaseDC(hWnd,hDC))                    // Are We Able To Release The DC
    {
        MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
        hDC=NULL;                                        // Set DC To NULL
    }


    if (hWnd && !DestroyWindow(hWnd))                    // Are We Able To Destroy The Window?
    {
        MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
        hWnd=NULL;                                        // Set hWnd To NULL
    }


    if (!UnregisterClass("OpenGL",hInstance))            // Are We Able To Unregister Class
    {
        MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
        hInstance=NULL;                                    // Set hInstance To NULL
    }
}


void OpenGL_Display::DrawNewImage(int width, int height, unsigned char* imageData, bool flip)
{
    GLenum mError = glGetError();
    
    BOOL returnVal = wglMakeCurrent(hDC, hRC);
    mError = glGetError();
    
    UpdateGLBuffers(width, height, imageData);    
    DrawGLScene(flip);
    SwapBuffers(hDC);
}


extern "C" __declspec(dllexport) void OpenGLDisplay_Init(int width, int height, int posX, int posY)
{
    oglDisp.Init_Display(width, height, posX, posY);
    
    GLenum mError = glGetError();
}


extern "C" __declspec(dllexport) void OpenGLDisplay_Wrapper(unsigned char* image, int width, int height, bool flip)
{
    oglDisp.DrawNewImage(width, height, image, flip);    
}

.h:

#ifndef __OPENGLDISPLAY
#define __OPENGLDISPLAY
#include <windows.h>        // Header File For Windows
#include <stdio.h>            // Header File For Standard Input/Output
#include <gl\gl.h>            // Header File For The OpenGL32 Library
#include <gl\glu.h>            // Header File For The GLu32 Library
#include <process.h>


namespace OGD
{
    class OpenGL_Display
    {
        public:
            OpenGL_Display();
            ~OpenGL_Display();
            void Init_Display(int width, int height, int posX, int posY);
            void DrawNewImage(int width, int height, unsigned char* imageData, bool flip);
            bool CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag, int posX, int posY);
            GLvoid ReSizeGLScene(GLsizei width, GLsizei height);
            int LoadGLTextures();
            int InitGL(GLvoid);
            void UpdateGLBuffers(int width, int height, unsigned char* imageData);
            int DrawGLScene(bool flip);
            GLvoid KillGLWindow(GLvoid);
            HDC            hDC;        // Private GDI Device Context
            
        protected:
            static LRESULT    CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);    // Declaration For WndProc


            HGLRC        hRC;        // Permanent Rendering Context
            HWND        hWnd;        // Holds Our Window Handle
            HINSTANCE    hInstance;        // Holds The Instance Of The Application


            bool    keys[256];            // Array Used For The Keyboard Routine
            bool    active;        // Window Active Flag Set To TRUE By Default
            bool    fullscreen;    // Fullscreen Flag Set To Fullscreen Mode By Default
            bool    g_bExitThread; // end thread
            GLuint    texture;            // Bitmap image buffer
            int        g_iCounter;        // Current buffer being grabbed to
            bool    g_GLdone;    // Counter to make sure no unnecessary updates are made which cause CPU load
    };
};


extern "C" __declspec(dllexport) void OpenGLDisplay_Wrapper(unsigned char* image, int width, int height, bool flip);
extern "C" __declspec(dllexport) void OpenGLDisplay_Init(int width, int height, int posX, int posY);


static OGD::OpenGL_Display oglDisp;
#endif

I had to call wglMakeCurrent(NULL, NULL); from the initiation now it works fine.