Click here to Skip to main content
11,411,494 members (67,083 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Win32
Hi,
I am trying to create a win32 screen saver. I know a bit of windows programming.
Could you please help me how do i actually make necessary modifications of my win32 xyz.c file so that I could load it as a successful screen saver.

Code from OP posted in comments:

/********************************************************************************
*                                                                               *
* Name: Code rain                                                               *
* Date: 11/08/2012                                                              *
* Author: Kiran Bhatter.                                                        *
* Version: 0.1                                                                  *
* Source: C                                                                     *
*                                                                               *
********************************************************************************/
#include <windows.h>
#include <tchar.h>
#include <scrnsave.h>

const TCHAR szSaverName[] = _T("example");
HWND hWnd = NULL;
int cxClient = 0, cyClient = 0;
 
LONG WINAPI ScreenSaverProc(HWND, UINT, WPARAM, LPARAM);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    MSG msg;
    WNDCLASSEX wc = {0};
 
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_VREDRAW | CS_HREDRAW;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpfnWndProc = ScreenSaverProc;
    wc.hInstance = hInstance;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szSaverName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, _T("RegisterClassEx() failed!"), szSaverName, MB_ICONERROR);
        return 1;
    }
 
    hWnd = CreateWindowEx(0, szSaverName, szSaverName, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
 
    if(!hWnd)
    {
        MessageBox(NULL, _T("CreateWindowEx() failed!"), szSaverName, MB_ICONERROR);
        return 1;
    }
 
    ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd);
 
    while(1)
    {
        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                break;
 
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
 
    UnregisterClass(szSaverName, hInstance);
 
    return msg.wParam;
}
 
LONG WINAPI ScreenSaverProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    switch(iMsg)
    {
        case WM_CREATE:
            return 0;
 
        case WM_SIZE:
            cxClient = LOWORD(lParam);
            cyClient = HIWORD(lParam);
            return 0;
 
        case WM_KEYDOWN:
            switch(wParam)
            {
                case VK_ESCAPE:
                    SendMessage(hWnd, WM_DESTROY, 0, 0);
                    break;
            }
            return 0;
 
        case WM_CLOSE:
        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
            return 0;
    }
    
    return DefScreenSaverProc(hWnd, iMsg, wParam, lParam);
}
Posted 11-Aug-12 8:31am
Edited 11-Aug-12 14:12pm
v2
Comments
Wes Aday at 11-Aug-12 13:36pm
   
A simple google search should tell you all you need to know. What exactly is the problem?
kiranbhatter at 11-Aug-12 13:49pm
   
When I actually try to return DefScreenSaverProc from ScreenSaverProc the compiler throws message that it not recognized.

I have included and i linked the .lib file aswell.

Can you help ?
I am using visual studio 2010
Tarun Y Mangukiya at 11-Aug-12 14:39pm
   
Just google it...
Volynsky Alex at 11-Aug-12 15:02pm
   
and maybe it:
http://www.go4expert.com/forums/showthread.php?t=23154
Volynsky Alex at 11-Aug-12 19:01pm
   
Linker Tools Error LNK2001

unresolved external symbol "symbol"

Code references something (such as a function, variable, or label) that the linker can't find in the libraries and object files.

Possible causes

What the code asks for doesn't exist (the symbol is spelled incorrectly or uses the wrong case, for example).
The code asks for the wrong thing (you are using mixed versions of the libraries, some from one version of the product, others from another version).

This error message is followed by fatal error LNK1120.

Specific causes

Coding Problems

If the LNK2001 diagnostic text reports that __check_commonlanguageruntime_version is an unresolved external symbol, see LNK2019 for information on how to resolve.
The definition of member template is outside the class. Visual C++ has a limitation in which member templates must be fully defined within the enclosing class. See KB article Q239436 for more information about LNK2001 and member templates.
Mismatched case in your code or module-definition (.def) file can cause LNK2001. For example, if you named a variable var1 in one C++ source file and tried to access it as VAR1 in another.
A project that uses function inlining yet defines the functions in a .cpp file rather than in the header file can cause LNK2001.
Calling a C function from a C++ program without using extern "C" (which causes the compiler to use the C naming convention) can cause LNK2001. Compiler options /Tp and /Tc cause the compiler to compile files as C or C++, respectively, regardless of the filename extension. These options can cause function names different from what you expect.
Attempting to reference functions or data that don't have external linkage can cause LNK2001. In C++, inline functions and const data have internal linkage unless explicitly specified as extern.
A missing function body or variable can cause LNK2001. With just a function prototype or extern declaration the compiler can continue without error, but the linker cannot resolve a call to an address or reference to a variable because there is no function code or variable space reserved.
Calling a function with parameter types that do not match those in the function declaration can cause LNK2001. Name decoration incorporates the parameters of a function into the final decorated function name.
Incorrectly included prototypes, which cause the compiler to expect a function body that is not provided can cause LNK2001. If you have both a class and non-class implementation of a function F, beware of C++ scope-resolution rules.
When using C++, including a function prototype in a class definition and failing to include the implementation of the function for that class can cause LNK2001.
Attempting to call a pure virtual function from the constructor or destructor of an abstract base class can cause LNK2001. A pure virtual function has no base class implementation.
Trying to access a static variable from outside the file in which it is declared can cause LNK2001. Functions declared with the static modifier by definition have file scope. Static variables have the same limitation.
Trying to use a variable declared within a function (a local variable) outside the scope of that function can cause LNK2001.
Trying to use a global constant in C++ in multiple files can cause LNK2001. In C++, unlike C, global constants have static linkage. To get around this limitation, you can include the const initializations in a header file and include that header in your .cpp files, or you can make the variable non-constant and use a constant reference to access it.
When building a Release version of an ATL project, indicates that CRT startup code is required. To fix, do one of the following,
Remove _ATL_MIN_CRT from the list of preprocessor defines to allow CRT startup code to be included. See General Configuration Settings Property Page for more information.
If possible, remove calls
kiranbhatter at 13-Aug-12 14:30pm
   
Ok! I have renamed the ScreenSaverProc to WndProc and also renamed the DefScreenSaverProc to DefWindowProc after which I have renamed the generated xyz.exe to xyz.scr and finally copied the xyz.scr to "C:\WINDOWS\SYSTEM32" folder. Now I can see the xyz screen saver in the drop down list of screen saver settings! But is this the right way of adding a screen saver?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
kiranbhatter at 11-Aug-12 15:16pm
   
I have been following these articles and still I am having some problems during compilation :(
kiranbhatter at 11-Aug-12 15:19pm
   
1>ScrnSave.lib(scrnsave.obj) : error LNK2005: _WinMain@16 already defined in MyMain.obj
1>ScrnSave.lib(scrnsave.obj) : error LNK2001: unresolved external symbol _ScreenSaverProc@16
1>ScrnSave.lib(scrnsave.obj) : error LNK2001: unresolved external symbol _ScreenSaverConfigureDialog@16
1>ScrnSave.lib(scrnsave.obj) : error LNK2001: unresolved external symbol _RegisterDialogClasses@4
1>c:\documents and settings\administrator\my documents\visual studio 2010\Projects\MySaver\Debug\MySaver.exe : fatal error LNK1120: 3 unresolved externals
kiranbhatter at 11-Aug-12 15:22pm
   
And here is my code.

/******************************************************************************************
* *
* Name: Code rain *
* Date: 11/08/2012 *
* Author: Kiran Bhatter. *
* Version: 0.1 *
* Source: C *
* *
******************************************************************************************/
#include <windows.h>
#include <tchar.h>
#include <scrnsave.h>

const TCHAR szSaverName[] = _T("example");
HWND hWnd = NULL;
int cxClient = 0, cyClient = 0;

LONG WINAPI ScreenSaverProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
MSG msg;
WNDCLASSEX wc = {0};

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpfnWndProc = ScreenSaverProc;
wc.hInstance = hInstance;
wc.lpszMenuName = NULL;
wc.lpszClassName = szSaverName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("RegisterClassEx() failed!"), szSaverName, MB_ICONERROR);
return 1;
}

hWnd = CreateWindowEx(0, szSaverName, szSaverName, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, hInstance, NULL);
if(!hWnd) {
MessageBox(NULL, _T("CreateWindowEx() failed!"), szSaverName, MB_ICONERROR);
return 1;
}

ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);

while(1) {
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if(msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClass(szSaverName, hInstance);
return msg.wParam;
}

LONG WINAPI ScreenSaverProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
switch(iMsg) {
case WM_CREATE:
return 0;

case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;

case WM_KEYDOWN:
switch(wParam) {
case VK_ESCAPE:
SendMessage(hWnd, WM_DESTROY, 0, 0);
break;
}
return 0;

case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
return 0;
}
return DefScreenSaverProc(hWnd, iMsg, wParam, lParam);
}
kiranbhatter at 11-Aug-12 18:10pm
   
see! i have already browsed through the google before me posting onto this site.
Richard MacCutchan at 12-Aug-12 4:15am
   
You have a WinMain() in your code which is not required, you also have a definition for ScreenSaverProc() which is pre-defined in scrnsave.h. See the links I suggested below, for some useful sample code.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Linker Tools Error LNK2005

symbol already defined in object

The given symbol, displayed in its decorated form, was multiply defined.

Possible causes

Accidentally linking with both the single-threaded and multithreaded libraries. Ensure that the application project file includes only the appropriate libraries and that any third-party libraries have appropriately created single-threaded or multithreaded versions.
Mixing static and dynamic libraries when also using /clr.
The symbol is a packaged function (created by compiling with /Gy) and was included in more than one file but was changed between compilations. Recompile all files that include symbol.
The symbol is defined differently in two member objects in different libraries, and both member objects were used.
An absolute is defined twice, with a different value in each definition.
A header file declared and defined a variable. Possible solutions include:
Declare the variable in .h: extern BOOL MyBool; and then assign to it in a .c or .cpp file: BOOL MyBool = FALSE;.
Declare the variable static.
Declare the variable selectany.
If you use uuid.lib in combination with other .lib files that define GUIDs (for example, oledb.lib and adsiid.lib). For example:

oledb.lib(oledb_i.obj) : error LNK2005: _IID_ITransactionObject
already defined in uuid.lib(go7.obj)

To fix, add /FORCE:MULTIPLE to the linker command line options, and make sure that uuid.lib is the first library referenced.
More information look here:
http://msdn.microsoft.com/en-us/library/72zdcz6f%28v=vs.71%29.aspx[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Soe useful sample code can be found here[^], and Microsoft offer some samples[^] also.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 223
1 Sascha Lefévre 155
2 Sergey Alexandrovich Kryukov 149
3 ProgramFOX 90
4 Dave Kreskowiak 90
0 Sergey Alexandrovich Kryukov 8,920
1 OriginalGriff 7,128
2 Maciej Los 3,440
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 12 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100