Introduction: USB Lock

About: I had a long break in my online activity, bad things happened in my life and i'm still trying to recover, sorry folks that i was away for a long time and please be patient with me, i will be better in time, i …

I had the need to have a switch to turn read only mode for all my USB sticks (sadly only a old 128Mb one had a hardware switch). The use of SD and microSD cards with a adaptor (they both have a switch) is a option but only if your PC has a card reader. After some research i came up with a plan. And after some testing, and codeing i have made this litlle aplication that fit my needs.

UPDATED 17.09.2013

!!! I MESSED THE WINDOWS 7 COMPATIBILITY, SORRY FOLKS PLEASE USE A VERSION BEFORE 1.5.0.0 FOR VISTA+ SO !!!

Rewriten in Visual C++ 2008

Version: 1.5.0.0
Download: https://docs.google.com/file/d/0ByKfzUUEN5s-ZmRnZ19Gbk1vYXc/edit?usp=sharing
[File is in a 7zip archive www.7-zip.org]

Version 1.5.1.0
Download: https://docs.google.com/file/d/0ByKfzUUEN5s-Z2M4SndNNjFqN28/edit?usp=sharing
[File is in a 7zip archive www.7-zip.org]

Version 1.5.1.5
Download: https://docs.google.com/file/d/0ByKfzUUEN5s-anhaNEMzQ0hwXzQ/edit?usp=sharing
[File is in a 7zip archive www.7-zip.org]

Step 1: How It Does What It Does ?

It sets in the registry a dword value to 1 for "Read-Only Mode" and 0 for "Read-Write Mode".

Path:
HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Control -> StorageDevicePolicies

Key
WriteProtect=1 or 0

I have added 2 registry files that enable or disable that, for learnig purpose.

Step 2: The Aplication

Ok some info about it :

It is writen in Autoit3 , this is a nice scripting language similar to Visual Basic .NET and has no depndency librarys needed to be installed to run. The executable is standalone.

After starting it you will see a lock icon with a green dot in tray area. (green is Read-Write Mode and red is Read-Only Mode).

Right click it and there you have the option to lock or unlock the about option and last, exit option.

Version 1.0.0.0 The base version, see Step 3 for more info.
(1st file for download)

Version 1.1.0.0 Added devcon.exe (part of Windows XP WAIK) with "binary inject" to get the same exe file working even under Vista and Win 7. Now it automaticaly restarts the USB device, without the need to unplug and replug of the phisical device.
On some AV software it will be seen as a potentaial virus (IT IS A FALSE POSITIVE)
(2nd file for download)

Version 1.2.0.0 Removed the binary inject and embeded devcon.exe and made a custom autorun window closer closer. This version lost its compatibility with Vista and Win 7. Its gonna get fixed in the next version.
(3nd file for download)

Version 1.2.5.0 The closeing feature of autorun windows is now included in the main program. The devcon.exe got packed for a smaller size.
(4nd file for download)

Version 1.2.6.0 Now closeing all window class 32770 (this is dialog window class) this way it hits the autoplay windows but sadly it hits the MS Spy++ window too, sometimes even the Hamachi chat window. This bug is gonna be fixed soon too.
(5nd file for download)

Version 1.3.0.0 Included the binary inject back and now we got the compatibility with Vista and Win 7 back too. Improved the autorun closeing feature and some minor performance tweaks.
(6nd file for download)

Version 1.3.5.0 Now it trys to restart only if there is a usb removable device pluged in. If not it just skips that part. The autorun window closeing feature it is executed only if there is a device present. The RAM memory usage is reduced with dynamic memory reduce that checks the usage evry 1 second and does the necesary adjustments. From 8mb average it goes down to 600k.
(7nd file for download)

Version 1.3.9.0 Added command line parameters -install and -uninstall to automaticaly start USB Lock.exe with windows. Useing a GUID now it limits the running of the application only a single istance.
(8nd file for download)

Version 1.3.9.5 Added the autostart of USB Lock with windows in tray menu (it has persistent settings storage only for this option).
The program now it rename itself back to the original name "USB Lock.exe".
(9nd file for download)

Version 1.4.0.0 Fixed a stupid bug under Windows XP that made the app crach right after it change state.
(last file for download)

Version 1.4.0.0 Fixed a freaky error that apeared after windows update on windows xp when you switch to read-only or read-write mode and no usb storage device is present.
(10th file for download)

Version 1.5.0.0 The code is completly rewriten in Microsoft Visual C++ 2008, it still usees the "binary inject" tech to make it work under windows vista, 7 and 8, the autorun and write mode is now persistent, you can set read-only mode then quit the app and it will stay that way and when you start the app again it will show you the curent state. The command line arguments are cuted out as the baloon tooltips, but thy will be back with more features. Single dependency lib is MSVCR90.dll. Here is the redist if you dont have it already (http://www.microsoft.com/en-us/download/details.aspx?id=29) The now filesize is a nice 41k.
Download from my gdrive (https://docs.google.com/file/d/0ByKfzUUEN5s-ZmRnZ19Gbk1vYXc/edit?usp=sharing)
[File is in a 7zip archive www.7-zip.org]
Instructables makes some problems with uploading more files to this instructable :S.

Version 1.5.1.0 Fixed some minor errors, added change of state. from read only to read write and back, on double click on main icon.
Download: https://docs.google.com/file/d/0ByKfzUUEN5s-Z2M4SndNNjFqN28/edit?usp=sharing
[File is in a 7zip archive www.7-zip.org]

Version 1.5.1.5 Fixed a memory leak :P and made some more optimisations. Fixed a random apearing error on windows 7.
Download: https://docs.google.com/file/d/0ByKfzUUEN5s-anhaNEMzQ0hwXzQ/edit?usp=sharing
[File is in a 7zip archive www.7-zip.org]

Step 3: The Source Code

Here is the source code:

Images display it with sintax highlight.

It requires Admin rights to run (it writes in the registry).
I have tested it on windows XP (OEM, SP1, SP2 and SP3).
When it is closed it automaticaly enables write mode.
If you kill the app from task manager in the loked state usb will remain in read-only mode. (rerun app and after a lock and unlok it will be fixed.
IT DOES NOT LOCK USB STICKS THAT ARE CURENTLY PLUGED IN. YOU NEED TO REINSERT THEM AFTER LOCKING TO HAVE READ-ONLY MODE. (Fixed this with version 1.1.0.0)

Icons are from IconArchive.com