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...
moonlit Posted December 11, 2006 Share Posted December 11, 2006 In my infinite boredom I thought I'd have a crack at this myself: USBLock v1.1 This tool locks the workstation when you remove the device. http://www.freewebs.com/5kah/USBLock.rar - Binary, Win32 http://www.freewebs.com/5kah/Lock.rar - Source, Visual Basic 6 USBLogoff v1.1 Pretty much the same as USBLock, except that instead of locking the machine it logs off. http://www.freewebs.com/5kah/USBLogoff.rar - Binary, Win32 http://www.freewebs.com/5kah/Logoff.rar - Source, Visual Basic 6 Edit: Updated to v1.1, Logoff.dat no longer required. Quote Link to comment Share on other sites More sharing options...
moonlit Posted December 11, 2006 Share Posted December 11, 2006 USBShowShutdown v1.1 Displays the standard Windows shutdown box upon removal of the USB storage device. http://www.freewebs.com/5kah/USBShow - Binary, Win32 http://www.freewebs.com/5kah/USBDisplayShutdown.rar - Source, Visual Basic 6 Edit: Updated to v1.1, Logoff.dat no longer required. Quote Link to comment Share on other sites More sharing options...
moonboy33 Posted December 11, 2006 Share Posted December 11, 2006 @goldfish You could use Sleep(1000) command instead of the waitOneSecond-function. (you need to include windows.h to do this) //edit: It would also be good to hide the window! HWND hwnd = FindWindow("ConsoleWindowClass",NULL); ShowWindow(hwnd,SW_HIDE); edit2: Like this: http://pastebin.ca/275576 Quote Link to comment Share on other sites More sharing options...
goldfish Posted December 11, 2006 Author Share Posted December 11, 2006 You could use Sleep(1000) command instead of the waitOneSecond-function.(you need to include windows.h to do this) That's what I was trying to avoid :) I wanted to keep it as bloat free as possible. Also, you can hide the console window from the way it's spawned - for example the auto run VBS file. Removes the need for any windows api at all. I'm sorry, I just hate windows api :p EDIT: Having said that there is quite a minimal ammount of bloat just for hiding the window. That works for me! :) Now... about this API for the locking... Quote Link to comment Share on other sites More sharing options...
SomeoneE1se Posted December 11, 2006 Share Posted December 11, 2006 now all we need is a way to unlock when the drive is reinserted.. Quote Link to comment Share on other sites More sharing options...
goldfish Posted December 11, 2006 Author Share Posted December 11, 2006 Now that is the challenge which we havn't got to just yet ;) But I'm working on it. Quote Link to comment Share on other sites More sharing options...
moonboy33 Posted December 12, 2006 Share Posted December 12, 2006 Also, you can hide the console window from the way it's spawned Could you maybe give me an example? Quote Link to comment Share on other sites More sharing options...
goldfish Posted December 13, 2006 Author Share Posted December 13, 2006 Actually it's occurred to me you don't need to even do that. You can simply tell your compiler not to compile in the open new window directive - in Dev-C++ you can uncheck a box for MingW. Quote Link to comment Share on other sites More sharing options...
Falcon Posted December 14, 2006 Share Posted December 14, 2006 here ya go, one already made http://sourceforge.net/projects/usl/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.