Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C#
Tip/Trick

Disable Screensaver

Rate me:
Please Sign up or sign in to vote.
4.88/5 (6 votes)
13 Nov 2010CPOL1 min read 36.8K   7   3
Disables the screensaver even on machines where it's not allowed due to domain policy.



Introduction


In many workplaces, the domain policy will not allow you to disable your screensaver. Often it's not even possible to adjust the time before the screensaver sets in. When the screensaver starts, you will have to login to the workstation again using ctrl+alt+del and then enter domain credentials.



When using multiple workstations, both physical and virtual, this process can be very time consuming and annoying during the work day, if you e.g. have to login to different machines every third minute.


Disable Screensaver is a small light utility that runs in the background and makes sure that the screensaver will not be activated automatically.

Disable Screensaver does not require any installation. Just place the standalone executable DisableScreensaver.exe in a directory and run it.


Background


Disable Screensaver will query the default domain screensaver timeout value. Then behind the scenes, it will press the "Scroll Lock" button twice a second before the screensaver will start. This will reset the screensaver timeout value, and not in any way interfere with your workflow.



Using the Code


To be able to programatically press the "Scroll Lock" button, PInvoke is used:



[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);

private static void PressScrollLock()
{
    const byte vkScroll = 0x91;
    const byte keyeventfKeyup = 0x2;

    keybd_event(vkScroll, 0x45, 0, (UIntPtr)0);
    keybd_event(vkScroll, 0x45, keyeventfKeyup, (UIntPtr)0);
}


PInvoke is also used to query the domain policy for the sceensaver timout:



[DllImport("user32.dll")]
private static extern bool SystemParametersInfo(int uAction, int uParam, ref int lpvParam, int flags);

public static Int32 GetScreenSaverTimeout()
{
    Int32 value = 0;
    SystemParametersInfo(14, 0, ref value, 0);
    return value;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCrashes with a fault in KernelBase.dll when ScreenSaverTimeout = 0 Pin
Rene Vliex9-Jan-20 3:59
Rene Vliex9-Jan-20 3:59 
Generalso it doesnt disable screensaver, it just keeps it on wait T... Pin
Sherylee8-Nov-10 22:33
Sherylee8-Nov-10 22:33 
GeneralThanks For Sharing Pin
Robert Cowan15-Nov-10 22:00
Robert Cowan15-Nov-10 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.