Advertisement
TrunghieuTH10

USB_WM_DEVICECHANGE

Oct 21st, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 1.93 KB | None | 0 0
  1. #RequireAdmin
  2. Global $DBT_DEVNODES_CHANGED = 0x0007
  3. Global $DBT_DEVTYP_OEM = 0x00000000
  4. Global $DBT_DEVTYP_DEVNODE = 0x00000001
  5. Global $DBT_DEVTYP_VOLUME = 0x00000002
  6. Global $DBT_DEVTYP_PORT = 0x00000003
  7. Global $DBT_DEVTYP_NET = 0x00000004
  8. Global Const $WM_DEVICECHANGE = 0x0219
  9. Global Const $DBT_DEVICEARRIVAL = 0x8000
  10. Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004
  11. Global Const $GUI_RUNDEFMSG = 'GUI_RUNDEFMSG'
  12.  
  13.  
  14. $Gui = GUICreate("WM_DEVICECHANGE", 280, 100)
  15. $ControlID = GUICtrlCreateLabel('WM_DEVICECHANGE function is performed when connecting or disconnecting devices to the system.' & @CRLF & 'Try inserting the stick', 5, 5, 270, 70)
  16. GUISetState()
  17. GUIRegisterMsg($WM_DEVICECHANGE, "WM_DEVICECHANGE")
  18.  
  19. Do
  20. Until GUIGetMsg() = -3
  21.  
  22. Func WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
  23.     WinSetTitle($Gui, '', 'Device Connection')
  24.     If ($wParam = $DBT_DEVICEARRIVAL) Or ($wParam = $DBT_DEVICEREMOVECOMPLETE) Then
  25.         Local $DEV_BROADCAST_VOLUME = DllStructCreate("int dbcvsize;int dbcvdevicetype;int dbcvreserved;int dbcvunitmask;" & _
  26.                 "ushort dbcvflags", $lParam)
  27.         Local $iDriveType = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvdevicetype")
  28.     Else
  29.         Return $GUI_RUNDEFMSG
  30.     EndIf
  31.     ; If the device is not a logical drive, the output of the function
  32.     If $iDriveType <> $DBT_DEVTYP_VOLUME Then Return $GUI_RUNDEFMSG
  33.  
  34.     Local $iMask = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvunitmask")
  35.     $iMask = Log($iMask) / Log(2)
  36.  
  37.     Local $iDrive = Chr(65 + $iMask) & ":"
  38.     Switch $wParam
  39.         Case $DBT_DEVICEARRIVAL ; detection device
  40.             TrayTip("WM_DEVICECHANGE", "device connected", 5, 1)
  41.             GUICtrlSetData($ControlID, "device connected")
  42.  
  43.         Case $DBT_DEVICEREMOVECOMPLETE ; Disconnecting
  44.             TrayTip("WM_DEVICECHANGE", "device disconnected", 5, 2)
  45.             GUICtrlSetData($ControlID, "device disconnected")
  46.     EndSwitch
  47.  
  48.     Return $GUI_RUNDEFMSG
  49. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement