USB Lock


Introduction: USB Lock

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


Rewriten in Visual C++ 2008

[File is in a 7zip archive]

[File is in a 7zip archive]

[File is in a 7zip archive]

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

HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Control -> StorageDevicePolicies

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 The base version, see Step 3 for more info.
(1st file for download)

Version 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 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 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 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 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 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 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 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 Fixed a stupid bug under Windows XP that made the app crach right after it change state.
(last file for download)

Version 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 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 ( The now filesize is a nice 41k.
Download from my gdrive (
[File is in a 7zip archive]
Instructables makes some problems with uploading more files to this instructable :S.

Version Fixed some minor errors, added change of state. from read only to read write and back, on double click on main icon.
[File is in a 7zip archive]

Version Fixed a memory leak :P and made some more optimisations. Fixed a random apearing error on windows 7.
[File is in a 7zip archive]

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.

Icons are from



    • Water Contest

      Water Contest
    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest

    4 Discussions

    Nice idea!
    Here a little improvement that MAY work.
    Set this value to 4 to desactivate usb drive, then to 3 to activate again.

    So, you could modify your app to:
    Read only mode:
    1) set to read-only
    2) desactivate usb device (act like unplug)
    3) activate usb device (act like re-plugging)
    4) profit!

    Normal mode:
    1) set to normal mode (read/write)
    2) desactivate usb device (act like unplug)
    3) activate usb device (act like re-plugging)
    4) profit!

    2 replies

    Nice ideea, but registry editing on that key works only after restart, to simulate a unplug and a re-plug the only way i kow it can work it is with devcon.exe (Windows WAIK). This action has some steps :

    1. Get the device ID for all usb sticks pluged (automaticaly)
    2. after registry write, disale, wait 1-2 seconds and enable all usb sticks pluged
    3. I gonna work on this 2 get it working :D