AS COOL as .NET can be, there is a lot of functionality that remains to be built into it. However, as with everything in programming, where there's a will there's a way. In this brief article, I'm going to show you how to use the System.Runtime.Interopservices namespace to fire a machine's screensaver programmatically.
A few months ago, my company upgraded our domain controller and put some rigorous security provisions in place. Some of the enhancements were the use of really really really strong passwords and locking the machine automatically after 15 minutes of non-use. Well, just to satisfy my intellectual curiosity, I decided to try to come up with a program that could make my computer look busy all day right before the lockout. [In practice, I would never employ a solution like this because Network Administrators have a wide arsenal of cruel punishment tools to deal with programmers circumventing their security policy--- And I've learned the hard way.
Anyway, I first considered writing a program with a timer that would reset itself each time the mouse moved or there was a key_down event. For some reason, I just didn't like this approach. So I started trying to figure out what event was being polled to tell the system to lock itself after 1minute and 30 seconds or what seemed to be that short anyway. After a little digging around, I noticed that User32.dll could be used to send a message to the OS and fire the screensaver. Now, if you have ever used COM interop in .NET, you know it's not the cleanest looking stuff you've ever seen so I decided to create a class that would simply fire the screensaver after x minutes if it wasn't already running.
The first thing you need to do when trying to programmatically manipulate the API is import System.Runtime.Interopservices (and please, don't send me email pointing out that the first thing is turning on your computer, then opening the IDE etc ;-) - I know that already).
So, I created a class that looks roughly like the following:
| using System.Runtime.InteropServices; namespace ScreenSaver { public class ScreenSaver { [DllImport("User32.DLL")] public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam); public const Int32 WM_SYSCOMMAND = 0x112; public const Int32 SC_SCREENSAVE = 0xF140; public SceenSaver(IntPtr windHandle) { // // TODO: Add constructor logic here // SendMessage(windHandle , WM_SYSCOMMAND, SC_SCREENSAVE, 0); } } } |