Sign in to follow this  
Followers 0
tannis00

Selectively re-enabling Autorun on USB

3 posts in this topic

I don't know about the rest of you, but I liked the old autorun functionality. It was very useful for my portable apps USB drive. So... Thanks to several posts on this forum, I managed to put together the following AutoIt script. It doesn't look in the autorun.inf, but what it does do is to launch a specific program when a correctly named USB drive is inserted. Hope someone finds it useful...

; Autorun
;
; This program is a workaround for Microsoft's annoying disabling of the autorun feature for USB drives (rather than fixing the real
; issue.
; Once started, autorun.exe will continuously monitor for insertion of USB drives. If the drive inserted matches the drive name
; specified ("Portable", unless a different name is specified on the command line), then a prescribed program will be launched 
; from that drive (asuite.exe unless a different name is specified on the command line).
;

Global $New, $myPgm, $MyDrive

Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.

TraySetClick(16)    ; Only secondary mouse button will show the tray menu.

$showitem = TrayCreateItem("Show parameters")
TrayItemSetOnEvent(-1,"ShowParms")

$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"ExitScript")

TraySetState()

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$New = "[drive]:"
$MyDrive = "PORTABLES"
$myPgm = "\asuite.exe"

; Read command line parameters
If $cmdline[0] > "0" Then
    $MyDrive = $cmdLine[1]
EndIf
if $cmdLine[0] > "1" Then
    $myPgm = $cmdLine[2]
    if StringLeft($myPgm,1) <> "\" Then
        $myPgm = "\" & $myPgm
    EndIf
EndIf

; Setup drive insertion notification. This uses the Async method so that the exit menu command from the systray icon
; will work. Using the blocking form (ExecNotificationQuery) is easier on resources, but you can't exit until you insert
; a new USB drive.
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$EventSink = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($EventSink , "SINK_")

$objWMIservice.ExecNotificationQueryAsync ($EventSink, "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_DiskDrive'", Default, Default, Default)

While 1
WEnd

Exit

; Executed when a USB drive is inserted. If the drive name matches, then run the specified program from it.
Func SINK_OnObjectReady($objObject, $objAsyncContext)
    $obj = $objObject.TargetInstance
    If $obj.InterfaceType == "USB" Then
        $New = GetDriveLetterFromDisk($obj.Name)
        $Label = DriveGetLabel($New)
        If $Label == $MyDrive Then
            ShellExecute($New & $myPgm)
        EndIf
    EndIf
EndFunc

Func GetDriveLetterFromDisk($name)
    $ans = ""
    $name = StringReplace($name,"\","\\")
    $oq_part = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & $name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($oq_part) Then
        For $obj_part In $oq_part
            $oq_disk = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & $obj_part.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
            If IsObj($oq_disk) Then
                For $obj_disk in $oq_disk
                    $ans = $ans & $obj_disk.Name
                Next
            EndIf
        Next
    EndIf
    
    Return $ans
EndFunc

; Systray menu funtions follow
Func ShowParms()
    MsgBox(0, "Parameters", "Drive: " & $MyDrive & chr(13) & chr(10) & "Command: " & $New & $myPgm)
EndFunc

Func ExitScript()
    Exit
EndFunc
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$EventSink = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($EventSink , "SINK_")

$objWMIservice.ExecNotificationQueryAsync ($EventSink, "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_DiskDrive'", Default, Default, Default)

While 1
WEnd

Exit

Func SINK_OnObjectReady($objObject, $objAsyncContext)
    $obj = $objObject.TargetInstance
    If $obj.InterfaceType == "USB" Then
        $New = GetDriveLetterFromDisk($obj.Name)
        $Label = DriveGetLabel($New)
        If $Label == $MyDrive Then
            ShellExecute($New & $myPgm)
        EndIf
    EndIf
EndFunc

Func GetDriveLetterFromDisk($name)
    $ans = ""
    $name = StringReplace($name,"\","\\")
    $oq_part = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & $name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($oq_part) Then
        For $obj_part In $oq_part
            $oq_disk = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & $obj_part.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
            If IsObj($oq_disk) Then
                For $obj_disk in $oq_disk
                    $ans = $ans & $obj_disk.Name
                Next
            EndIf
        Next
    EndIf
    
    Return $ans
EndFunc

; Systray menu funtions follow
Func ShowParms()
    MsgBox(0, "Parameters", "Drive: " & $MyDrive & chr(13) & chr(10) & "Command: " & $New & $myPgm)
EndFunc

Func ExitScript()
    Exit
EndFunc

Share this post


Link to post
Share on other sites



Very nice. with a few modifications it can be turned into an autorun.inf remover.

Share this post


Link to post
Share on other sites

You duplicated the code in your posting, it's in there twice causing all sorts of trouble when trying to run it.

Another thing, put a small sleep in your While...Wend loop, your script is eating up about 50% CPU usage without at least a Sleep(10) in there.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0