Thanks, I've copied the example script from that site, and I can get this to work:
#include <GUIConstantsEx.au3>
Global Const $WM_DEVICECHANGE = 0x0219
Global Const $DBT_DEVICEARRIVAL = 0x8000 ; Found New Hardware
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004 ; The device is disconnected
Global Const $DBT_DEVNODES_CHANGED = 0x0007
Global Const $DBT_DEVTYP_OEM = 0x00000000 ; oem-defined device type
Global Const $DBT_DEVTYP_DEVNODE = 0x00000001 ; devnode number
Global Const $DBT_DEVTYP_VOLUME = 0x00000002; logical partition
Global Const $DBT_DEVTYP_PORT = 0x00000003 ; serial, parallel
Global Const $DBT_DEVTYP_NET = 0x00000004 ; network resource
Global $k = 0
$Gui = GUICreate("WM_DEVICECHANGE", 370, 140)
GUICtrlCreateLabel('WM_DEVICECHANGE function is performed when connecting or disconnecting devices to the system. Try inserting the stick', 5, 5, 360, 70)
GUISetState()
GUIRegisterMsg($WM_DEVICECHANGE, "WM_DEVICECHANGE")
Do
Until GUIGetMsg() = -3
Func WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
$k += 1
WinSetTitle($Gui, '', 'Connection ' & $k)
If ($wParam = $DBT_DEVICEARRIVAL) Or ($wParam = $DBT_DEVICEREMOVECOMPLETE) Then
Local $DEV_BROADCAST_VOLUME = DllStructCreate("int dbcvsize;int dbcvdevicetype;int dbcvreserved;int dbcvunitmask;" & _
"ushort dbcvflags", $lParam)
Local $iDriveType = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvdevicetype")
Else
Return $GUI_RUNDEFMSG
EndIf
; If the device is not a logical drive, the output of the function
If $iDriveType <> $DBT_DEVTYP_VOLUME Then Return $GUI_RUNDEFMSG
Local $iMask = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvunitmask")
$iMask = Log($iMask) / Log(2)
Local $iDrive = Chr(65 + $iMask) & ":"
Switch $wParam
Case $DBT_DEVICEARRIVAL ; detection device
TrayTip("WM_DEVICECHANGE", "device connected", 5, 1)
Case $DBT_DEVICEREMOVECOMPLETE ; Disconnecting
TrayTip("WM_DEVICECHANGE", "device disconnected", 5, 2)
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
This updates the tray message when a device is connected & disconnected.
But when I try using the extra functions you have mentioned I don't get anywhere... :
#include <GUIConstantsEx.au3>
Global Const $WM_DEVICECHANGE = 0x0219
Global Const $DBT_DEVICEARRIVAL = 0x8000 ; Found New Hardware
Global Const $DBT_DEVICEREMOVECOMPLETE = 0x8004 ; The device is disconnected
Global Const $DBT_DEVNODES_CHANGED = 0x0007
Global Const $DBT_DEVTYP_OEM = 0x00000000 ; oem-defined device type
Global Const $DBT_DEVTYP_DEVNODE = 0x00000001 ; devnode number
Global Const $DBT_DEVTYP_VOLUME = 0x00000002; logical partition
Global Const $DBT_DEVTYP_PORT = 0x00000003 ; serial, parallel
Global Const $DBT_DEVTYP_NET = 0x00000004 ; network resource
Global $k = 0
$Gui = GUICreate("WM_DEVICECHANGE", 370, 140)
GUICtrlCreateLabel('WM_DEVICECHANGE function is performed when connecting or disconnecting devices to the system. Try inserting the stick', 5, 5, 360, 70)
GUISetState()
GUIRegisterMsg($WM_DEVICECHANGE, "WM_DEVICECHANGE")
Do
Until GUIGetMsg() = -3
Func WM_DEVICECHANGE($hWnd, $Msg, $wParam, $lParam)
$k += 1
WinSetTitle($Gui, '', 'Connection ' & $k)
If ($lParam = $DBT_DEVNODES_CHANGED) Then
Local $DEV_BROADCAST_VOLUME = DllStructCreate("int dbcvsize;int dbcvdevicetype;int dbcvreserved;int dbcvunitmask;" & _
"ushort dbcvflags", $lParam)
Local $iDriveType = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvdevicetype")
Else
Return $GUI_RUNDEFMSG
EndIf
; If the device is not a logical drive, the output of the function
If $iDriveType <> $DBT_DEVTYP_VOLUME Then Return $GUI_RUNDEFMSG
Local $iMask = DllStructGetData($DEV_BROADCAST_VOLUME, "dbcvunitmask")
$iMask = Log($iMask) / Log(2)
Local $iDrive = Chr(65 + $iMask) & ":"
Switch $lParam
Case $DBT_DEVNODES_CHANGED ; detection device
TrayTip("WM_DEVICECHANGE", "device connected", 5, 1)
;Case $DBT_DEVICEREMOVECOMPLETE ; Disconnecting
; TrayTip("WM_DEVICECHANGE", "device disconnected", 5, 2)
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc
For example here I have replaced some wParam objects with lParam objects and I am trying to trigger off $DBT_DEVNODES_CHANGED but nothing hapens when I connect/disconnect the device.... I'm not really sure what I am doing wrong here....
I was also looking at how you might run something like this as a service, and I found this >page which might be a good way to get the functions to run in the background. Would you say this is a good way to go? It seems pretty complicated, and since I'm confused with what I have so far, I am not sure how well I will get on with this other script
Thanks for the help so far!