How To Determine Whether a Screen Saver Is Running on Windows NT (150785)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 3.5
    • the operating system: Microsoft Windows NT 3.51
    • the operating system: Microsoft Windows NT 4.0

This article was previously published under Q150785

SUMMARY

On Windows 2000 and later versions, you can call the SystemParametersInfo function with the SPI_GETSCREENSAVERRUNNING flag to determine whether a screen saver is running on the system.

On Windows NT 4.0 and earlier versions, there is no direct method to determine whether a screen saver is running on the system. However, the indirect approach to determine whether a screen saver is running is to test for the existence of a desktop named "screen-saver". The "screen-saver" desktop is created by the Winlogon process for the screen saver to execute on. This desktop is created when the screen saver starts and is destroyed when the screen saver is 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;
}


/*

   IsScreenSaverRunning
      Returns TRUE if a screen saver is running; FALSE, otherwise.

*/ 
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"), 
                          0,
                          FALSE,
                          MAXIMUM_ALLOWED);
   if(hDesktop == NULL) 
   {
      // 
      // If the call fails due to access denied, the screen saver
      // is running because the specified desktop exists -- you just
      // don't have any access.
      if(GetLastError() == ERROR_ACCESS_DENIED)
      {
         return TRUE;
      }

      // 
      // Otherwise, indicate the screen saver is not running.
      return FALSE;
   }

   // 
   // Successfully opened the desktop (the screen saver is running).
   CloseDesktop(hDesktop);
   return TRUE;
} 
				

MORE INFORMATION

When you use the OpenDesktop() function to try to open the desktop named "screen-saver", three scenarios can result that help you to determine whether a screen saver is running:
  • The OpenDesktop() call succeeds. Close the returned desktop handle with the CloseDesktop(). 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.

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbScreenSaver KB150785