Jump to content



Recommended Posts


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:


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.


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.


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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...