I am trying to build a screen saver in windows with OpenGl . The followin is my main.cpp file that contain the main screen saver code:

#include <windows.h>

#include <scrnsave.h>

#include <commctrl.h>

#include <gl\gl.h> // Header for OpenGL32 Lib

#include <gl\glu.h> // GLu32 Lib

#include <gl\glaux.h> // GLaux Lib

#include "resource.h"

// Include these libraries

#pragma comment(lib, "OpenGL32.lib")

#pragma comment(lib, "GLu32.lib")

#pragma comment(lib, "GLaux.lib")

#pragma comment(lib, "ScrnSave.lib")

#pragma comment(lib, "comctl32.lib")

// Screen saver library contains the main function and other

// Startup code required for a scrnsaver user defined vars

#define TIMER 1

// Function Prototypes

// NOTE: In InitGL, hDC and hRC are passed by reference

// because both will be altered by this function.

// At least that is my understanding!

void InitGL(HWND hWnd, HDC &hDC, HGLRC &hRC); // Setup OpenGL pixelformat

void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC); // Clean up and close OpenGL

void SetupAnimation(int Width, int Height); // Setup for OpenGL

void OnTimer(HDC hDC); // Draw OpenGL Scene

// Global variables

int Width, Height; // Size of screen variables

// Screensaver procedure...first when window is created:

// 1) get users window resolution

// 2) call a function that sets up OpenGL

// 3) Setup a TIMER that will be used to drive animations


UINT message,

WPARAM wParam,

LPARAM lParam)


static HDC hDC; // Handle to device context

static HGLRC hRC; // Handle to OpenGL rendering context

static RECT rect; // Instance of structure RECT which defines the

// coords of the upper-left and lower-right

// corners of a rectangle



// Set timer and any other initializations


GetClientRect(hWnd, &rect); // Get window dimensions

Width = rect.right; // Store width

Height = rect.bottom; // Store height

InitGL(hWnd, hDC, hRC); // Initalize OpenGL

// Setup for OpenGL (look at graphics.cpp)

SetupAnimation(Width, Height);

// Create timer with timeout value (10)

SetTimer(hWnd, TIMER, 10, NULL);

return 0;

case WM_DESTROY: // Destroy timer and perform cleanup

KillTimer(hWnd, TIMER); // Destroy timer

CloseGL(hWnd, hDC, hRC); // Clean up and close OpenGL

return 0;

case WM_TIMER: // Perform drawing operations

OnTimer(hDC); // Draw OpenGL scene (graphics.cpp)

return 0;


// Unprocessed messages are handled by the screen saver

// library by calling the following:

return DefScreenSaverProc(hWnd, message, wParam, lParam);


// The system will call the following function when the user is

// in the control panel setting up the screensaver and they

// press the setting button...

BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg,

UINT message,

WPARAM wParam,

LPARAM lParam)


// Dialog message handling






case IDOK:

EndDialog(hDlg, LOWORD(wParam) == IDOK);

return true;


EndDialog(hDlg, LOWORD(wParam) == IDOK);

return true;



return false;


// The following function registers any nonstandard window

// classes required by the screen saver if the screen saver

// does not require this functionality simply return true

BOOL WINAPI RegisterDialogClasses(HANDLE hInst)


return true;


// Initialize OpenGL

void InitGL(HWND hWnd, HDC &hDC, HGLRC &hRC)


// Next define pixelformat...we use stuct PIXELFORMATDESCRIPTOR

// This is straight out of NeHe¡¯s framework

static PIXELFORMATDESCRIPTOR pfd = // static ensures data

// is stored between calls



1, // nVersion should be set to 1

PFD_DRAW_TO_WINDOW | // buffer can draw to window

PFD_SUPPORT_OPENGL | // buffer supports OpenGL drawing

PFD_DOUBLEBUFFER, // buffer is double buffered

PFD_TYPE_RGBA, // rgba pixels

24, // 24-bit color depth

0, 0, 0, 0, 0, 0, // look up rest at MSDN, color bits ignored

0, // no alpha buffer

0, // shift bit ignored

0, // no accumulation buffer

0, 0, 0, 0, // accumulation bits

16, // 16 bit z buffer

0, // no stencil buffer

0, // no auxiliary buffer

PFD_MAIN_PLANE, // main drawing layer

0, // reserved

0, 0, 0 // layer mask ignored


hDC = GetDC(hWnd); // Retrieves a handle to a

// display device context

int i = ChoosePixelFormat(hDC, &pfd); // Try and match a pixel format

// supported by DC

SetPixelFormat(hDC, i, &pfd);

hRC = wglCreateContext(hDC); // Create a new OpenGL rendering context

wglMakeCurrent(hDC, hRC); // Makes OpenGL hRC the calling

// threads current RC


// Shut down OpenGL

void CloseGL(HWND hWnd, HDC hDC, HGLRC hRC)


wglMakeCurrent(NULL, NULL); // Makes current RC no longer current

wglDeleteContext(hRC); // Deletes OpenGL rendering context

ReleaseDC(hWnd, hDC); // Releases a DC, freeing it

// for other apps


when i try to compile the program i have the following error:


1>scrnsave.lib(scrnsave.obj) : error LNK2019: unresolved external symbol _ScreenSaverProc@16 referenced in function _RealScreenSaverProc@16

1>C:\Documents and Settings\User\Desktop\Reem\CS527\Screen Saver\wn_scr\test4\Debug\test4.exe : fatal error LNK1120: 1 unresolved externals

Any help please!!!

Note: i m using visual studio 2005 + VC++ 8 and windowsXP.


Re: Visual C++ General ScreenSaver with VC++ 8

Prasad Somwanshi

#pragma comment(lib, "ScrnSave.lib")

For Unicode build this should be Scrnsavw.lib.

Modify this to,

#ifdef UNICODE

#pragma comment(lib, "ScrnSavw.lib")


#pragma comment(lib, "ScrnSave.lib")


Re: Visual C++ General ScreenSaver with VC++ 8


Thank you for your reply..

but i tried it and still i have the same error!!!..

any other suggestions

Re: Visual C++ General ScreenSaver with VC++ 8

Prasad Somwanshi

Are you sure, you have changed lib name for UNICODE.

Because, I can compile this code without any problem.

Re: Visual C++ General ScreenSaver with VC++ 8


Well i figure out what was the problem..

i was adding the scrnsave.lib in the linker option.. i remove it and change the lib name for UNICOD and its work now.

Thanks for your help

Re: Visual C++ General ScreenSaver with VC++ 8

Prasad Somwanshi

Mark previous post as answer, and help to close this thread.