Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Disable Screensaver

4.88/5 (6 votes)
13 Nov 2010CPOL1 min read 37.6K  
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)