Screen Saver Enabler/Disabler

Legend: func = function; prog = program; ptr = pointer; SS = Screen Saver; var = variable; Win = Windows;

This code enables / disables the current active screen saver through a call to the SytemParametersInfo function. This code also demonstrates pointer usage in CheckSSStatus() for programmers who are learning pointers or who need a review.

Check out the Onbuttonenable(), Onbuttondisable() & CheckSSStatus() funcs. This code should be fairly straightforward.

This is only my second Win32 prog, also only my second prog in C++/VC++. When I last programmed, I was using Pascal on a 386 SX-16 w/ MS-DOS 5.0 & Win 3.1!

You may use this code in any manner you wish, but I request an email notifying me of the use of the code and a return email address so I can reply to you.

Please send any improvements, bug reports, complaints or modifications you may have...as I am a beginner. My name is Marc and my email is: mehowe@yahoo.com. You will be credited if I use the modifications in any program I write.

This program was written because I burn CD-R's and the SS should be disabled while doing so to avoid transfer interruptions. I didn't want to have to go to my desktop and right-click to open the display properties or open the Control Panel from the start menu. Really, I just wanted to see if I could write a little utility to disable or enable a SS.

I did not find an article already on CodeGuru dealing with this issue, so here is the code.

This code should work under UNICODE as well. I have only tested it on two separate machines running Win '98. According to MSDN documentation, this code will work on NT 4.0. I am still considering switching to NT 4.0...maybe.

This code was written in the MSVC++ 6.0 Pro Edition (w/SR-2 update).

Overview of code:
NOTE: In SSED's project settings, I used MFC as a Statically Linked Library. This causes the compiler to give the following two warnings when set to the Level 4 error checking state:
LINK : warning LNK4089: all references to "SHELL32.dll" discarded by /OPT:REF
LINK : warning LNK4089: all references to "comdlg32.dll" discarded by /OPT:REF

If you receive either of these warnings, you can ignore them (according to documentation). This is just letting you know that the MFC is being used as a Statically Linked Library.

Now, the real stuff. First, the InitDialog() func calls the CheckSSStatus() func to determine the current SS Status and update the dialog's edit box with the SS state's value.

Next, if the Enable button is pressed, it calls the Onbuttonenable() func. The Onbuttonenable() func creates a local boolean ptr var named *pvParam...this is the name given to the parameter in the MSDN SystemParametersInfo() documentation. Next, the Win SystemParametersInfo() func is called with the SPI_GETSCREENSAVEACTIVE parameter and is passed a reference to pvParam as &pvParam. This func is called to get the SS current state. Next, the if construct tests pvParam to see if it is set to TRUE, if it is, then the function does nothing and exits. If pvParam is set to FALSE, then it calls the Win SystemParametersInfo() func passing it the SPI_SETSCREENSAVEACTIVE parameter and setting it to TRUE...meaning that the last SS the user used is now enabled. Lastly, a call to the CheckSSStatus() func is made to test whether the call to the SPI_SETSCREENSAVEACTIVE() func performed properly.

The Disable button calls the Onbuttondisable() func and works in the same manner as the Onbuttonenable() func, except sending the FALSE value to disable the current SS.

The CheckSSStatus() func initializes in basically the same manner as the above functions. However, it also sets a CString member var named m_strEditSSStatus to "ENABLED" or "DISABLED" depending on the SS current status and then sends it the the dialog's edit box. It then updates the dialog's display to show the user the current SS state.



void CSSEDDlg::OnButtonenable()
{
 // TODO: Add your control notification handler code here
 bool *pvParam; //declare local bool ptr variable for the SystemParametersInfo func()

 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &pvParam, 0); //Get SS state
 if (pvParam != FALSE)
 {
  // If SS already enabled, do nothing
 } //end if
 // If SS enabled, disable it
 else if (pvParam == FALSE)
 {
  SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, 0, 0);
  //Enable screen saving
  //SPIF_SENDWININICHANGE (SystemParametersInfo Parameter #4 alternative, see MSDN documentation)
  
  //Call CheckSSStatus() func to check performance of SystemParameterInfo() func
  //& update the dialog's display with the SS status

  CheckSSStatus();
 } //end else if
} //End OnButtonEnable()

/Onbuttondisable() func is same, except FALSE is set...see SSEDdlg.cpp for details. void CSSEDDlg::CheckSSStatus(void) { bool *pvParam; //declare local bool ptr variable for the SystemParametersInfo func() SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &pvParam, 0); if (pvParam == FALSE) { // If SS disabled, send "DISABLED" to read-only edit control and update the display m_strEditSSStatus = "DISABLED"; //m_EditSSStatus is a member Cstring var for the dialog's edit box UpdateData(FALSE); } //end if else if (pvParam != FALSE) { // If SS enabled, send "ENABLED" to read-only edit control and update the display m_strEditSSStatus = "ENABLED"; //m_EditSSStatus is a member Cstring var for the dialog's edit box UpdateData(FALSE); } //end if } //end CheckSSStatus()

LAST NOTES:
LIMITATIONS:
1. (Intentional) Once a button is pushed, the SS enable/diable action is carried out...no way to cancel. This prog was meant to be simple and small. The user must use the other button to reset the SS or go to the display properties via desktop or Control Panel to change it back.
2. (Intentional) No OK or CANCEL buttons as ESC key or the [X] button on the system title bar will close the prog. Program does not eat the ESC key intentionally.
SYSTEM REQUIREMENTS: Appears to use about 200K RAM when running. May run on any Win'9x or Win NT system...see "Tested On" below.
TESTED ON: Two Pentium II machines running Win'98. These machines are also running the freeware CPU cooler utility "RAIN"--highly recommended for anyone using Win'9x to keep their CPU cool. Win NT 4.0 already has a built-in CPU cooling routine as part of its OS. Neither system has experienced any conflicts with RAIN, ever! I am in no way affiliated with the RAIN programmers, I just recommend it for anyone using a Win '9x Pentium II (or better) machine as these CPU's get incredibly HOT!
LEGAL/WARRANTY: This software is (C) Copyrighted 1999 by Marc E. Howe, All Rights Reserved. The author makes no warranties, either expressed or implied regarding the use of any of the SSED software. The user is wholly responsible for any and all damages resulting from the use of this software. The author is fully free from all liability regarding the use of this software by the consumer. You may use this code in any manner you wish, but the author requests an email notifying him of the use of the code and a return email address so the author can reply. By using this software, you agree to all of the terms in the above legal / warranty statements.

Download demo project - 97 KB

Date Last Updated: April 24, 1999



Comments

  • Screen Saver Enabler/Disabler

    Posted by Legacy on 11/10/1999 12:00am

    Originally posted by: Dave Wolfson

    Marc's article is the only one I've found regarding the mysterious:
    warning LNK4089: all references to "SHELL32.dll" discarded by /OPT:REF

    Is this another Microsoft-ism? Meaning, hey, ignore it, Microsoft will not correct the problem?

    Reply
  • Code Correction

    Posted by Legacy on 04/29/1999 12:00am

    Originally posted by: Marc Howe -- Author

    The following code needs to be corrected.  I misused the pointers and references in the OnButtonenableSS(),
    OnButtondisableSS() & CheckSSStatus() funcs.  Forgive me as I am new to this type of programming.
    
    

    The following code was corrected by Kunz Dieter...Thanks!

    void CWinMgrDlg::OnButtondisabless()
    {
    // TODO: Add your control notification handler code here

    BOOL pvParam; //declare local bool variable for the SystemParametersInfo func()

    BOOL l_RetVal_b = SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &pvParam, 0);

    // the function call was ok - only in this case the pvParam holds valid data
    if(l_RetVal_b)
    {
    if (pvParam == FALSE){
    //If SS already disabled, do nothing
    } //end if

    //If SS enabled, disable it
    else if (pvParam != FALSE){

    //Disable screen saving capability for the current user session
    SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, 0, 0);

    //Call CheckSSStatus() func to check performance of SystemParameterInfo() func
    //& update the dialog's display with current SS status

    CheckSSStatus();
    }

    } //end top if
    } //End OnButtondisabless()

    Note: The topmost if() statement should be added & BOOL implementation should be corrected in all three functions. I used "bool" instead of "BOOL" typedef for the SystemParametersInfo() func and did not give the func a return value for testing success. Don't forget to add the BOOL l_RetVal_b to all three funcs as well.

    Good luck & thanks to all,

    Marc

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Cisco and Intel have harnessed flash memory technology and truly innovative system software to blast through the boundaries of today's I/O-bound server/storage architectures. See how they are bringing real-time responsiveness to data-intensive applications—for unmatched business advantage. Sponsored by Cisco and Intel® Partnering in Innovation

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds