How to programmatically stop screen saver from starting

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.
LVL 30
AxterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
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
novitiateCommented:
try
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, false, 0, 0);


_novi_
0
AxterAuthor Commented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

DarrylshCommented:
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
AxterAuthor Commented:
>>Here you go and the related, monitor energysaving powerdown

Exactly how, or in what context, would I be able to use above code?
0
DarrylshCommented:
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
AxterAuthor Commented:
>>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
DarrylshCommented:
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
DarrylshCommented:
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
AxterAuthor Commented:
>>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
DarrylshCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AxterAuthor Commented:
>>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
DarrylshCommented:
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
DarrylshCommented:
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
AxterAuthor Commented:
>>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
DarrylshCommented:
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
AxterAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.