Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How do I know when a USB device is inserted?

by ZJ.Mike.2009 (Scribe)
on Mar 06, 2010 at 05:28 UTC ( [id://827095]=perlquestion: print w/replies, xml ) Need Help??

ZJ.Mike.2009 has asked for the wisdom of the Perl Monks concerning the following question:

I want my Perl script to know when a USB device is inserted and which new volume letter appear on my Windows XP. I've done a little of googling and I've found something that looks relevant to my question:
1. The system broadcasts a set of default device change events to all +application. 2. Any application with a top-level window can receive basic notificat +ions by processing the WM_DEVICECHANGE message. 3. The DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events are autom +atically broadcast to all top-level windows. Volume notifications are + also broadcast to top-level windows.
My question is:
1. If I create a simple GUI application using Win32::GUI, will this ap +plication also receive DBT_DEVICEARRIVAL message sent by Windows syst +em? 2. If so, how do I look at the message. I checked the Event method of +Win32::GUI and there're GetMessage() and PeekMessage() methods. Are t +hey going to see the DBT_DEVICEARRIVAL message? 3. Or are there some other approaches, for example, using Win::API to +import the WM_ONDEVICECHANGE function and then do some stuff?
Thanks for your attention :)

Replies are listed 'Best First'.
Re: How do I know when a USB device is inserted?
by Corion (Patriarch) on Mar 06, 2010 at 05:55 UTC

    I use (and wrote) DBD::WMI to monitor the addition/removal of devices, but using it in a GUI program means that you'll have to use a second thread that listens to the WMI.

    If you're using Win32::GUI, then yes, those messages will get sent to your top level window as well.

    Looking through Win32::GUI, resp. the Win32::GUI examples, I see listview_drag_drop.pl, which hooks a message (WM_CONTEXTMENU) to a callback. I didn't find it documented anywhere, but it seems that it is a/the way to specify callbacks to window messages in Win32::GUI. I guess that the following code should issue the callback whenever a device changes:

    use constant WM_ONDEVICECHANGE => 0x219; # as per http://msdn.microsof +t.com/en-us/library/aa363480%28VS.85%29.aspx $window->Hook(\&device_changed); sub device_changed { ... };

      Corion, thanks a lot for the information:)

      I think I'll try the DBD::WMI module first and I'll see what I can do with a GUI interface.

      Thanks again :)

        Here's a working example:

        #!perl -w use strict; use Win32::GUI (); use constant WM_DEVICECHANGE => 0x219; # as per http://msdn.microsoft.com/en-us/library/aa363480%28VS.85%29.a +spx sub device_changed { my ($win, @args) = @_; Win32::GUI::MessageBox(0,"Device changed: @args\n"); }; my $mw = Win32::GUI::Window->new(); $mw->Hook(WM_DEVICECHANGE, \&device_changed); $mw->Show(); Win32::GUI::Dialog(); print "Done\n";

        A problem with this approach is that WM_DEVICECHANGE does not seem to fire if AutoPlay is disabled for devices, at least I don't get the message when I insert a CF card in my CF card reader, while I get the message when I plug/unplug some other device.

Re: How do I know when a USB device is inserted?
by grantm (Parson) on Mar 07, 2010 at 21:38 UTC

    This is not directly relevant to your situation since you're running Windows, but I thought I'd answer anyway in case a Linux user comes across this question.

    On Linux you can use the Net::DBus module to to subscribe to notifications from HAL (the Hardware Abstraction Layer). The Net::DBus::GLib module allows easy integration with a Gtk app without needing to use threads. An example of an app that does this is my usb-key-copy-con a Perl GUI tool for automated copying of USB storage devices.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://827095]
Approved by zwon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found