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>
BOOL show_last_error()
{
char pcError[255];
DWORD dw_err=GetLastError();
DWORD dw=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw_err,
GetSystemDefaultLangID(),
pcError,
sizeof(pcError)-1,
NULL);
if(dw==0)
return FALSE;
else
{
MessageBox(NULL,pcError,"Error",MB_OK|MB_ICONERROR);
return TRUE;
}
}
BOOL launch_selected_screensaver()
{
HKEY hk_key_handle=NULL;
LONG lret;
lret=RegOpenKeyEx(HKEY_CURRENT_USER,
"Control Panel\\Desktop" ,
0L,
KEY_READ,
&hk_key_handle
);
if(lret!=ERROR_SUCCESS)
return FALSE;
DWORD dw_buff_size=MAX_PATH;
char out_buffer[MAX_PATH];
DWORD dwdatatype=0;
lret = RegQueryValueEx(hk_key_handle,
"SCRNSAVE.EXE",
NULL,
&dwdatatype,
(byte*)out_buffer,
&dw_buff_size
);
RegCloseKey(hk_key_handle);
if(lret!=ERROR_SUCCESS)
return FALSE;
ShellExecute(NULL, "open", out_buffer, NULL, NULL, SW_SHOWNORMAL);
return TRUE;
}
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);
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;
}
do
{
strncpy(ppc_table_screensaver[cnt],s_file.name,MAX_PATH);
cnt++;
}
while((_findnext(hFile,&s_file)==0)&&(cnt<MAX_SCREEN_SAVER));
_findclose( hFile );
srand(GetTickCount());
index=(rand()*cnt)/RAND_MAX;
if(index>=cnt)
index=cnt-1;
sprintf(pc_screensaver,"%s\\%s",pc_system_dir,ppc_table_screensaver[index]);
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;
}
|
|