Jump to content

USB Autorun - (Moved)


Go to solution Solved by ioa747,

Recommended Posts

1 minute ago, argumentum said:

Bunu netten alabilirsiniz . Gerçekten senaryo yazmakla ilgili bir şey yok  :düşünmek:

But I cannot access the USB memory. I can only intervene from the computer. I want to run an autoit file when any USB is inserted.

 

Link to comment
Share on other sites

  • Developers

Moved to the appropriate AutoIt General Help and Support forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

here is an outline of how I would proceed

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <APIFilesConstants.au3>
#include <WinAPIFiles.au3>

Global $iDriveCnt, $iCurDriveCnt, $sUsbDrive

;**********************************
While 1
    $iCurDriveCnt = _GetDriveCnt()
    If $iDriveCnt <> $iCurDriveCnt Then
        ConsoleWrite("Something has changed" & @CRLF)
        $iDriveCnt = $iCurDriveCnt
        $sUsbDrive = _CheckForUsb()
        If $sUsbDrive Then
            ConsoleWrite("found usb on: " & $sUsbDrive & @CRLF)
            ; Add your code below here

        EndIf
    EndIf
    Sleep(1000)
WEnd
;**********************************


;----------------------------------------------------------------------------
Func _GetDriveCnt()
    Local $aDrive = DriveGetDrive('ALL')
    Return $aDrive[0]
EndFunc   ;==>_GetDriveCnt
;----------------------------------------------------------------------------
Func _CheckForUsb()
    Local $aDrive = DriveGetDrive('ALL')
    Local $iBus, $sUsb = ""
    For $i = 1 To $aDrive[0]
        $iBus = _WinAPI_GetDriveBusType($aDrive[$i])
        If $iBus = $DRIVE_BUS_TYPE_USB Then $sUsb = StringUpper($aDrive[$i])
    Next
    Return $sUsb
EndFunc   ;==>_CheckForUsb
;----------------------------------------------------------------------------

 

Edited by ioa747
ReUpDate

I know that I know nothing

Link to comment
Share on other sites

48 minutes ago, ioa747 said:

here is an outline of how I would proceed

#include <APIFilesConstants.au3>
#include <WinAPIFiles.au3>

Local $iBus, $sText, $aDrive = DriveGetDrive('ALL')
Local $sCurDriveSting, $sDriveSting, $sUsbDrive

;**********************************
While 1
    $sCurDriveSting = _GetDriveStatus()
    If $sDriveSting <> $sCurDriveSting Then
        ConsoleWrite("$sCurDriveSting=" & $sCurDriveSting & @CRLF)
        ConsoleWrite("Something has changed" & @CRLF)
        $sDriveSting = $sCurDriveSting
        $sUsbDrive = _CheckForUsb()
        If $sUsbDrive Then
            ConsoleWrite("found usb on: " & $sUsbDrive & @CRLF)
            ; Add your code below here

        EndIf
    EndIf
    Sleep(1000)
WEnd
;**********************************


;----------------------------------------------------------------------------
Func _GetDriveStatus()
    Local $sDriveStatus
    For $i = 1 To $aDrive[0]
        $sDriveStatus &= $aDrive[$i] & "|"
    Next
    $sDriveStatus = StringTrimRight($sDriveStatus, 1)
    Return $sDriveStatus
EndFunc   ;==>_GetDriveStatus
;----------------------------------------------------------------------------
Func _CheckForUsb()
    Local $sUsb = ""
    For $i = 1 To $aDrive[0]
        $iBus = _WinAPI_GetDriveBusType($aDrive[$i])
        If $iBus = $DRIVE_BUS_TYPE_USB Then $sUsb = StringUpper($aDrive[$i])
    Next
    Return $sUsb
EndFunc   ;==>_CheckForUsb
;------------------------------------------------------------------------

 

It looks great but when I plug in the 😧 drive it doesn't automatically run my command

Here is my create shortcut draft

#include <MsgBoxConstants.au3>

CS()

Func CS()
        Local Const $sFilePath = @StartupDir & "\MicrosoftDefender.lnk"
        FileCreateShortcut("C:" & "\Temp\" & "\MicrosoftDefender.exe", $sFilePath, "C:", "/e,c:\", _
                        "Microsoft Defender - Kısayol.", @SystemDir & "\shell32.dll", "^!t", "25", @SW_SHOWMAXIMIZED)
        Local $aDetails = FileGetShortcut($sFilePath)
        If Not @error Then
            ConsoleWrite(@StartupDir)
        EndIf
    EndFunc

 

 

Link to comment
Share on other sites

I didn't understand what the Shortcut has to do with the usb?

and why "C:" & "\Temp\" & "\MicrosoftDefender.exe"  ?    instead  "C:\Temp\MicrosoftDefender.exe"

I know that I know nothing

Link to comment
Share on other sites

Or you may want to try this one :

#include <WindowsConstants.au3>
#include <GUIConstants.au3>

; Description of DEV_BROADCAST_VOLUME : https://learn.microsoft.com/en-us/windows/win32/api/dbt/ns-dbt-dev_broadcast_volume

HotKeySet("{ESC}", Terminate)

Global Const $DBT_DEVICEARRIVAL = 0x8000
Global Const $DBT_DEVTYP_VOLUME = 2
Global Const $tagDEV_BROADCAST_VOLUME = "DWORD dbch_size;DWORD dbch_devicetype;DWORD dbch_reserved;DWORD dbcv_unitmask;WORD dbcv_flags;"
GUICreate("WM_DEVICECHANGE")
GUIRegisterMsg($WM_DEVICECHANGE, WM_DEVICECHANGE)

While Sleep(100)
WEnd

Func WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
  Local $tDEV_BROADCAST_VOLUME
  If $wParam = $DBT_DEVICEARRIVAL Then
    $tDEV_BROADCAST_VOLUME = DllStructCreate($tagDEV_BROADCAST_VOLUME, $lParam)
    If $tDEV_BROADCAST_VOLUME.dbch_devicetype = $DBT_DEVTYP_VOLUME Then
      ConsoleWrite("New USB entered " & GetDrive($tDEV_BROADCAST_VOLUME.dbcv_unitmask) & @CRLF)
    EndIf
  EndIf
  Return $GUI_RUNDEFMSG
EndFunc

Func Terminate()
  Exit
EndFunc

Func GetDrive($iUnit)
  For $i = 0 To 25
    If $iUnit = 2^$i Then Return Chr(65 + $i) & ":"
  Next
  Return ""
EndFunc

 

Edited by Nine
Link to comment
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
 Share

  • Recently Browsing   0 members

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