HomeHome
border border Developer Documentation border border
We just look at the registry key "HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE" to retrive the current screen saver.
If this key doesn't exist, we find all screen saver files (*.scr) located in the system directory, and we launch a random one.

Full small source code :
#include <windows.h>
#include <io.h>
#include <stdio.h>
//-----------------------------------------------------------------------------
// Name: show_last_error
// Object: show last windows api error
// Parameters :
//     in  :
//     out :
//     return : TRUE on success, FALSE on error
//-----------------------------------------------------------------------------
BOOL show_last_error()
{
    char pcError[255];
    DWORD dw_err=GetLastError();
    DWORD dw=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                           NULL,
                           dw_err,
                           GetSystemDefaultLangID(),//GetUserDefaultLangID()
                           pcError,
                           sizeof(pcError)-1,
                           NULL);
    
	
    //If the function succeeds, the return value is the number of TCHARs stored in the output buffer,
    //  excluding the terminating null character.
    //If the function fails, the return value is zero
    if(dw==0)
        return FALSE;// FormatMessage failed
    else
    {
        MessageBox(NULL,pcError,"Error",MB_OK|MB_ICONERROR);
        return TRUE;
    }
}

//-----------------------------------------------------------------------------
// Name: launch_selected_screensaver
// Object: retrive configured screen saver in registry and launch it
// Parameters :
//     in  :
//     out :
//     return : TRUE on success, FALSE on error
//-----------------------------------------------------------------------------
BOOL launch_selected_screensaver()
{
    HKEY hk_key_handle=NULL;
    LONG lret;
    // open key
    lret=RegOpenKeyEx(HKEY_CURRENT_USER,           // handle to open key
                         "Control Panel\\Desktop" ,// subkey name
                         0L,                       // reserved
                         KEY_READ,                 // security access mask
                         &hk_key_handle            // out handle to open key
                         );
    if(lret!=ERROR_SUCCESS)
        return FALSE;
    DWORD dw_buff_size=MAX_PATH;
    char out_buffer[MAX_PATH];
    DWORD dwdatatype=0;
    // read key
    lret = RegQueryValueEx(hk_key_handle,       // HKEY handle to key
                            "SCRNSAVE.EXE",     // LPCTSTR value name
                            NULL,               // LPDWORD reserved
                            &dwdatatype,        // LPDWORD type buffer
                            (byte*)out_buffer,  // LPBYTE data buffer
                            &dw_buff_size       // LPDWORD size of data buffer
                            );

    // close opened key
    RegCloseKey(hk_key_handle);
    if(lret!=ERROR_SUCCESS)
        return FALSE;
    // launch configured screen saver
    ShellExecute(NULL, "open", out_buffer, NULL, NULL, SW_SHOWNORMAL);
    return TRUE;
}
//-----------------------------------------------------------------------------
// Name: launch_rand_screensaver
// Object: find a screen saver in the system dir and launch it
// Parameters :
//     in  :
//     out :
//     return : TRUE on success, FALSE on error
//-----------------------------------------------------------------------------
void launch_rand_screensaver()
{
    #define MAX_SCREEN_SAVER 50
    struct _finddata_t s_file;
    long hFile;
    char pc_system_dir[MAX_PATH];
    char pc_msg[MAX_PATH];
    char pc_filter[MAX_PATH];
    char pc_screensaver[MAX_PATH];
    char ppc_table_screensaver[MAX_SCREEN_SAVER][MAX_PATH];
    int cnt=0;
    int index=0;

    if(!GetSystemDirectory(pc_system_dir,MAX_PATH))
    {
        show_last_error();
        return;
    }
    sprintf(pc_filter,"%s\\*.scr",pc_system_dir);
    
    // find first screen saver
    hFile = (long)_findfirst(pc_filter,&s_file);
    if(hFile==-1L)
    {
        sprintf(pc_msg,"No Screen saver (*.scr files) found in %s directory",pc_system_dir);
        MessageBox(NULL,pc_msg,"Error",MB_OK|MB_ICONERROR);
        return;
    }
    // find other screensaver
    do
    {
        strncpy(ppc_table_screensaver[cnt],s_file.name,MAX_PATH);
        cnt++;
    }
    while((_findnext(hFile,&s_file)==0)&&(cnt<MAX_SCREEN_SAVER));
    _findclose( hFile );

    // select a random screensaver
    srand(GetTickCount());
    index=(rand()*cnt)/RAND_MAX;
    if(index>=cnt)
        index=cnt-1;

    // make full path
    sprintf(pc_screensaver,"%s\\%s",pc_system_dir,ppc_table_screensaver[index]);
    // launch screen saver
    ShellExecute(NULL, "open", pc_screensaver, NULL, NULL, SW_SHOWNORMAL);
}

int __stdcall WinMain(HINSTANCE hInstance,
            HINSTANCE hPrevInstance,
            LPSTR lpCmdLine,
            int nCmdShow)
{
    UNREFERENCED_PARAMETER(hInstance);
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(nCmdShow);

    if(!launch_selected_screensaver())
        launch_rand_screensaver();

    return ERROR_SUCCESS;
}
Top
border border border border