#include <windows.h>
#define ERROR_NUMBER_ARGS -1
#define ERROR_CREATING_SEMAPHORE -2
#define SEMAPHORE_NAME "DISABLE_SCREENSAVER_SEMAPHORE"
BOOL show_last_error()
{
char pcError[255];
DWORD dw=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,NULL,
GetLastError(),GetSystemDefaultLangID(),
pcError,sizeof(pcError)-1,NULL);
if (dw==0)
return FALSE;// FormatMessage failed
else
{
MessageBox(NULL,pcError,"Error",MB_OK|MB_ICONERROR);
return TRUE;
}
}
void show_usage()
{
MessageBox(NULL,"The use of this software is the following:\r\n
DisableScreenSaver.exe /ApplicationName\r\n
where \"ApplicationName\" is the full path of the application you want to launch",
"Information",MB_OK|MB_ICONINFORMATION);
}
// disable screensaver
void disable_screensaver()
{
BOOL b_res;
b_res=SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
if (!b_res)
show_last_error();
b_res=SystemParametersInfo(SPI_SETLOWPOWERACTIVE,0,NULL,0);
if (!b_res)
show_last_error();
b_res=SystemParametersInfo(SPI_SETPOWEROFFACTIVE,0,NULL,0);
if (!b_res)
show_last_error();
}
void launch_and_wait_process(char* pc_fullpath)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// create directory path
char pc_application_directory[MAX_PATH];
char* ptr_application_directory;
int len=(int)strlen(pc_fullpath);
if (len>0)
{
// remove / if any
if (pc_fullpath[0]=='/')
{
pc_fullpath=&pc_fullpath[1];
len--;
}
}
if (len>0)
{
// remove " at the begining of pc_fullpath if any
if (pc_fullpath[0]=='"')
{
pc_fullpath=&pc_fullpath[1];
len--;
}
}
if (len>0)
{
// remove " at the end of pc_fullpath if any
if (pc_fullpath[len-1]=='"')
pc_fullpath[len-1]=0;
}
strncpy(pc_application_directory,pc_fullpath,MAX_PATH);
char* ptr=strrchr(pc_application_directory,'\\');
if (ptr)
{
*ptr=0;
ptr_application_directory=pc_application_directory;
}
else
ptr_application_directory=NULL;
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
pc_fullpath, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
ptr_application_directory,// Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
show_last_error();
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return;
}
// Wait until child process exits.
// WaitForSingleObject( pi.hProcess, INFINITE );
// Wait until child process exits and assume
// screen saver is disable every minute (min screen saver enabling)
DWORD dwRes=WAIT_TIMEOUT;
while (dwRes==WAIT_TIMEOUT)
{
dwRes=WaitForSingleObject( pi.hProcess, 59000 );
disable_screensaver();
}
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
int __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(nCmdShow);
BOOL b_res;
HANDLE hnd_semaphore;
DWORD dw_res_wait;
// check if there's arguments
if (strlen(lpCmdLine)==0)
{
show_usage();
return ERROR_NUMBER_ARGS;
}
//try to open semaphore
hnd_semaphore=OpenSemaphore(EVENT_ALL_ACCESS,FALSE,SEMAPHORE_NAME);
-
// if not named semaphore is not existing, create semaphore and disable screensaver
if (!hnd_semaphore)
{
// create semaphore
hnd_semaphore=CreateSemaphore(
NULL,// security attr
0,// initial count
0x7FFF,// max count
SEMAPHORE_NAME
);
if (!hnd_semaphore)
{
show_last_error();
return ERROR_CREATING_SEMAPHORE;
}
// disable screensaver
disable_screensaver();
}
else
{
b_res=ReleaseSemaphore(hnd_semaphore,1,NULL);
if (!b_res)
show_last_error();
}
// launch process and wait for it's end
launch_and_wait_process(lpCmdLine);
// WaitForSingleObject decrease the count of semaphores
// WaitForSingleObject time out --> there's no more other instance of disable_screensaver --> enable screensaver
dw_res_wait=WaitForSingleObject(hnd_semaphore,0);
if (dw_res_wait==WAIT_TIMEOUT)
{
// enable screensaver
b_res=SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, NULL, 0);
if (!b_res)
show_last_error();
b_res=SystemParametersInfo(SPI_SETLOWPOWERACTIVE,1,NULL,0);
if (!b_res)
show_last_error();
b_res=SystemParametersInfo(SPI_SETPOWEROFFACTIVE,1,NULL,0);
if (!b_res)
show_last_error();
}
// close semaphore handle
CloseHandle(hnd_semaphore);
return ERROR_SUCCESS;
}