Sign in to follow this  
Followers 0
c.haslam

Close a dialog when USB stick inserted - or cancelled

4 posts in this topic

Consider the following code:

Opt("GUIOnEventMode", 1)
Global $cancelled = False,$inserted = False

MyShowDialog()

Func MyShowDialog()
    $Form1 = GUICreate("Form1", 212, 92, 193, 115)
    GUICtrlCreateLabel("Waiting for you to insert USB stick", 16, 16, 165, 17)
    $cancel = GUICtrlCreateButton("Cancel", 120, 48, 65, 25, 0)
    GUICtrlSetOnEvent($cancel,"ClickedOnCancel")
    GUISetState(@SW_SHOW)

    While Not ($cancelled or $inserted)
        Sleep(100)
    WEnd
    GUIDelete($Form1)
EndFunc

Func ClickedOnCancel()
    $cancelled = True
EndFunc

How do I set $inserted to true when the user inserts a USB stick labelled "Documents"?


...chris

Share this post


Link to post
Share on other sites



Full example:

$bool = False
While 1
    Sleep(1000)
    If Not $bool Then
        $drives = DriveGetDrive("REMOVABLE")
        If $drives <> "" Then
            For $drive In $drives
                If DriveGetLabel($drive) == "Documents" Then
                    $bool = True
                    MsgBox(0, "Permission Granted", "The right USB stick was inserted!")
                    ExitLoop 2
                EndIf
            Next
        EndIf
    EndIf
WEnd

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Hi,

Opt('MustDeclareVars', 1)

MsgBox(0, 0, _getNewUSB())

Func _getNewUSB()
    Local $strComputer = ".", $objEvent = 0
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
    Local $colEvents = $objWMIService.ExecNotificationQuery _
            ("Select * From __InstanceOperationEvent Within 5 Where " _
             & "TargetInstance isa 'Win32_LogicalDisk'")
    While 1
        $objEvent = $colEvents.NextEvent
        If $objEvent.TargetInstance.DriveType = 2 Then
            If $objEvent.Path_.Class() = "__InstanceCreationEvent" Then Return $objEvent.TargetInstance.DeviceId
        EndIf
        Sleep(10)
    WEnd
EndFunc   ;==>_getNewUSB

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Many thanks to you both.

...chris


...chris

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