INFO: Detect If a Screen Saver Is Running on Windows NT
ID: Q150785
|
The information in this article applies to:
-
Microsoft Win32 Software Development Kit (SDK)
SUMMARY
In Windows NT, you may find it useful to determine whether a screen saver
is running on the system. On Windows NT 5.0 and greater, you can use the
SystemParametersInfo API and specify the SPI_GETSCREENSAVERRUNNING flag to
determine this.
In previous versions of Windows NT, there is no direct way to detect
whether a screen saver is running on the system. However, there is an
indirect approach to determine if a screen saver is running by testing for
the existence of a desktop named "screen-saver" that the winlogon component
of Windows NT creates for the screen saver to execute on. This desktop is
created and destroyed when the screen saver is started and stopped.
Sample Code
/**
The following sample illustrates how to detect if a screen saver
is running on Windows NT. This sample works by checking for the
existence of a desktop named "screen-saver". This desktop is
created dynamically by winlogon when a screen saver needs to be
launched.
**/
#include <windows.h>
#include <stdio.h>
BOOL IsScreenSaverRunning( void );
int
__cdecl
main(
void
)
{
if(IsScreenSaverRunning()) {
printf("Screen saver is running!\n");
}
else {
printf("Screen saver is NOT running!\n");
}
return 0;
}
//
// returns TRUE if a screen saver is running, or FALSE if not.
//
BOOL
IsScreenSaverRunning(
void
)
{
HDESK hDesktop;
//
// try to open the desktop that the screen saver runs on. This
// desktop is created on the fly by winlogon, so it only exists
// when a screen saver is invoked.
//
hDesktop = OpenDesktop(
TEXT("screen-saver"), // desktop name where screen saver runs
0,
FALSE,
MAXIMUM_ALLOWED // open for all possible access
);
if(hDesktop == NULL) {
//
// if the call fails due to access denied, the screen saver
// is running because the specified desktop exists - we just
// don't have any access.
//
if(GetLastError() == ERROR_ACCESS_DENIED) return TRUE;
//
// otherwise, indicate the screen saver isn't running
//
return FALSE;
}
//
// successfully opened the desktop (the screen saver is running)
//
CloseDesktop(hDesktop);
return TRUE;
}
MORE INFORMATION
When you use the OpenDesktop() Win32 API to attempt to open the desktop
named "screen-saver", three scenarios result that help you to determine
whether a screen saver is running:
- The OpenDesktop() call succeeds: Close the returned desktop handle with
the CloseDesktop() Win32 API. A screen saver is running.
- The OpenDesktop() call fails and GetLastError() indicates
ERROR_ACCESS_DENIED: The screen-saver desktop exists, but the caller
does not have access to the desktop. A screen saver is running.
- The OpenDesktop() call fails, and GetLastError() does not indicate
ERROR_ACCESS_DENIED: If the error is ERROR_FILE_NOT_FOUND, the desktop
does not exist and a screen saver is not running. Otherwise, an error
occurred attempting to open the desktop. A screen saver is not running.
Additional query words:
Keywords : kbcode kbnokeyword kbKernBase kbWinOS2000 kbDSupport GdiScrsav kbGrpKernBase
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbinfo