Jump to content
Sign in to follow this  
buckRogers

Need help with $DBT_DEVICEARRIVAL

Recommended Posts

buckRogers

Howdy,

I found some AutoIt code that detects USB Key insertion and identifies the drive letter.

I used part of the code and am able to capture ANY usb activity but seem to be getting default DEVICECHANGE notifications and

would like to know if it's possible to capture and identify a change to a USB printer?

CODE
$WM_DEVICECHANGE = 0x0219

GUICreate("")

GUIRegisterMsg($WM_DEVICECHANGE , "MyFunc")

Func MyFunc($hWndGUI, $MsgID, $WParam, $LParam)

msgbox(4096, "Device", $WM_DEVICECHANGE & " | " & $hWndGUI & " | " & $MsgID & " | " & $WParam & " | " & $LParam)

EndFunc

While 1

$GuiMsg = GUIGetMsg()

WEnd

When I run this and power on an already connected USB printer I get a message box with:

537 | 0x001C05BE | 537 | 0x00000007 | 0x00000000

I also get the same results (except with a different $hWndGUI) when I power off the printer. And also, same results when I insert a USB key or remove a USB key.

I've found that using $DBT_DEVICEARRIVAL = "0x00008000" and comparing $WParam to $DBT_DEVICEARRIVAL, I can get different results from $WParam and $LParam. I've also found out that $LParam is a pointer that contains data that can identify the device. Unfortunately, this won't work with a USB printer that is already connected and powered off. This detects a newly inserted device or removal.

I think that $DBT_DEVICEARRIVAL may have a different hex code that I can trap a power on/power off of a USB printer or at least find a better trap for a USB device change.

Overall use: I have users that plug in printers to different usb ports on their laptops/docks. Even with the same printer model it can create a different installation of the printer with the words (Copy 2) or higher number depending on the amount of Copy(s). I have written VBScript code that can clean up the Copy(s) printers and leave a one "good instance" of the printer for them to use. I need something that can stay TSR and monitor USB device changes and trigger the VBScript code clean up and found AutoIt to be my answer. I just need some help figuring out the specifics of the USB trap.

I appreciate any help that can be tossed my way...thanks, buckRogers

Share this post


Link to post
Share on other sites
GodlessSinner

Global $ID;= type your usb-drive ID here, you can get this by DriveGetSerial("DriveLetter:\")
While 1
    Sleep(1000)
$var = DriveGetDrive( "REMOVABLE" )
If NOT @error Then
    For $i = 1 to $var[0]
        If DriveGetSerial($var[$i]) = $ID Then Detected()
    Next
EndIf
WEnd
Func Detected()
;do something
EndFunc

Edited by Vitas

_____________________________________________________________________________

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  

×