?
Solved

How to programmatically stop screen saver from starting

Posted on 2005-03-29
17
Medium Priority
?
1,887 Views
Last Modified: 2013-12-03
How can I prevent the screensaver from starting up while my program is running.
Where I'm running this application, the users don't have the priviledges to disable the screensaver.
0
Comment
Question by:Axter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
17 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13657476
I think a dirty method could be to generate shift key keystrokes (alternating left and right to prevent MouseKeys to launch) in a secondary process.
0
 
LVL 8

Expert Comment

by:novitiate
ID: 13658869
try
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, false, 0, 0);


_novi_
0
 
LVL 30

Author Comment

by:Axter
ID: 13699662
novitiate,
>>SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, false, 0, 0);
That wouldn't work for my requirements, because this would still alow the screen saver to kick in before deactivating it.

jaime_olivares,
>>I think a dirty method could be to generate shift key keystrokes (alternating left and right to prevent MouseKeys to launch) in a secondary process.
I'm not sure how I would implement that.


I found a back handed way of closing the application that wouldn't allow me to change the screen saver settings.
After that, I'm just setting the screensaver timeout to a really large number.
      //Make sure screen saver timeout value is set to below value
      const UINT TimeOutValue = 5*24*60*60; //Set screen saver time for five days.  That should be enough time to process all the data
      UINT CurrentTimeOutValue = 0;
      SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, TimeOutValue, &CurrentTimeOutValue, NULL);
      if (CurrentTimeOutValue < TimeOutValue)
      {
            SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, TimeOutValue, NULL, NULL);
            Sleep(300);
            SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, TimeOutValue, &CurrentTimeOutValue, NULL);
            if (CurrentTimeOutValue < TimeOutValue)
            {
                  CString Msg;
                  Msg.Format("Could not change the screensaver timeout value.\nThis process can not continue without changing the screensaver timeout value to %u or greater.", TimeOutValue/60);
                  MessageBox(Msg);
                  return false;
            }
      }

This seems to be working, but I would like a cleaner method.

If no one post a better method by tomorrow, I'll request this question be deleted, or zero out the points to put it in the PAQ.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 3

Expert Comment

by:Darrylsh
ID: 13825539
Here you go and the related, monitor energysaving powerdown
case WM_SYSCOMMAND:      
            {
                  switch (wParam)      
                  {
                        
                        case SC_SCREENSAVE:      // Screensaver About Start
                        case SC_MONITORPOWER:// Monitor About to Powerdown
                        return 0;      // Prevent by returning 0 (false?)
                  }
                  break;      
            }
0
 
LVL 30

Author Comment

by:Axter
ID: 13825571
>>Here you go and the related, monitor energysaving powerdown

Exactly how, or in what context, would I be able to use above code?
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 13825613
Slight Mod to above code, else your program will freeze

case WM_SYSCOMMAND:
        {
            switch (wParam)
            {

            case SC_SCREENSAVE:      // Screensaver About Start
            case SC_MONITORPOWER:// Monitor About to Powerdown
                return 0;      // Prevent by returning 0 (false?)
            default:
            return DefWindowProc (hwnd, message, wParam, lParam);
            }
                       
            break;
        }
0
 
LVL 30

Author Comment

by:Axter
ID: 13825687
>>Slight Mod to above code, else your program will freeze

Sorry Darrylsh, but above code doesn't help me at all, with out usage information.
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 13825860
Oh I missed your comment, you would just put that in your windowsprocedure loop where you handle all your other messages.  One thing to note though, that it only works if your program has the focus
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 13825880
Here is the code I am using to test it with: (just the WindowProcedure, not Winmain())

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
       
    case WM_SYSCOMMAND:
        {
            switch (wParam)
            {

            case SC_SCREENSAVE:      // Screensaver About Start  
                return 0;      // Prevent by returning 0 (false?)
            default:
            return DefWindowProc (hwnd, message, wParam, lParam);
            }
                       
            break;
        }
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
0
 
LVL 30

Author Comment

by:Axter
ID: 13825900
>>One thing to note though, that it only works if your program has the focus

Then that will not work for my requirements, because the program doesn't have the focus.
It's monitoring and doing screen captures of another program that does have the focus, and the other program is a third party program that can not be modified.
0
 
LVL 3

Accepted Solution

by:
Darrylsh earned 2000 total points
ID: 13826163
This will disable it:

SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, FALSE,0,SPIF_SENDWININICHANGE);

But you will need to reable it before your program exits:

SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, TRUE, 0, SPIF_SENDWININICHANGE).
0
 
LVL 30

Author Comment

by:Axter
ID: 13826197
>>This will disable it:
>>SystemParametersInfo( SPI_SETSCREENSAVEACTIVE, FALSE,0,SPIF_SENDWININICHANGE);

As I stated in one of my previous comments, I can't use this method either, because this will allow the screen saver to kick in, before I turn it off.
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 13826206
It doesn't start the screensaver, maybe the difference is the the other suggestion had 0 for lat parameter, but this absolutely does not start the screensaver
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 13826219
Or maybe you are putting it in the wrong part of your program, you can put it write after WinMain() and not as a response to a windows message
0
 
LVL 30

Author Comment

by:Axter
ID: 13826259
>>It doesn't start the screensaver, maybe the difference is the the other suggestion had 0 for lat parameter, but this absolutely does not start the screensaver

So you're saying making this call will stop the screen saver from being active?

If so, I'll test it out, and report back later.
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 13826304
Yes that is what I am saying, I just tested it using just the first call and then not re-enabling it with the second and even after I exit the program, my screensaver has not come back on and it was set to 1 minute, so make sure to re-enable it or your users may be confused to why their sreensavers have stopped working
0
 
LVL 30

Author Comment

by:Axter
ID: 13870685
Thanks
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question