goldfish Posted December 11, 2006 Share Posted December 11, 2006 USB AUTO LOCKER Something which I put together out of my own interest. When I walk away from a machine - I'll lock the machine and take my USB drive with me. This simply streamlines the process and locks the workstation when I remove the drive. For some nice, nice reason, the forums won't let me post the code here. So for your reference, it's here: http://pastebin.ca/275123 Pretty simplistic. A couple of notes on it. 1) Program still runs after you remove the device This is why this works. If your program is loaded into memory it will stay there until it's finished, even if the drive it's "working" from doesn't exist any more. The only difference is you cannot read from or write to the device any more - this is how we detect it has been removed. 2) The System Call Hmph. I'm not happy with this. The easiest and cleanest way that I could see to lock the workstation is by calling rundll32 and invoking the LockWorkStation function in user32.dll. This is far from perfect - Ideally you would use runtime library loading to load user32 and use it's functions directly - but this adds a lot of extra crap that I didn't want to deal with. Also, you'll notice the start command. If you try to invoke an executable from a "working directory" which doesn't exist, you'll get an error ("The Current Directory is Invalid") ... EVEN if you specify the full path. START is a part of the system libraries, and as such doesn't require file access so the CWD isn't checked. Boom, it works. INSTALLATION Easiest method: Compile, copy to your USB device. When you insert the device, run this program. As soon as you remove the device your workstation will be locked. To make things a bit quicker, you could set your drive up to launch this on Autorun. This works with the ever popular U3 autorun hack - in fact I developed this using that as a testbed. TODO As much as I hate Windows API, it would probably be better to use it for this task. But since it's such a simple task what's the point in worrying about it? It probably needs a method of exiting the program BEFORE you remove the drive. As it stands if you're going to log out, you need to pull the drive, screen locks, you log back in again then log out. Not ideal. I'm thinking a simple keyboard hook that listens for a specific key combo, when it will exit. Since this will require WinAPI I might as well do the locking with WinAPI too. Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.