Microsoft KB Archive/150785

From BetaArchive Wiki

INFO: Detect If a Screen Saver Is Running on Windows NT

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:

  1. The OpenDesktop() call succeeds: Close the returned desktop handle with the CloseDesktop() Win32 API. A screen saver is running.
  2. 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.
  3. 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 : _IK kbnokeyword kbKernBase kbOSWin2000 kbDSupport kbGrpDSKernBase
Issue type : kbinfo
Technology : kbWin32SDKSearch kbAudDeveloper kbSDKSearch kbWin32sSearch


Last Reviewed: January 8, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.