views:

621

answers:

4

In Windows XP what is the best way to execute a particular application when a particular type of USB device is attached (it currently attaches as a storage device - i.e. it appears as a drive).

The solution I am looking for must execute the application from the very first time the device is attached or offer the application as a selection, whichever is easier to achieve, the device must remain attached as a storage device.

EDIT: Polling all attached devices is not adequate - windows will already have done its pop-ups at that stage. The issue is with starting the application without additional pop-ups, the application will then need to use the device as a normal storage drive.

+2  A: 

You can also turn on autoplay for USB drives, and setup an autorun.inf file on the USB drive, although I advise against this method as there are several viruses around that exploit this. There's a reason it's off by default.

If you do want to go down this road though, have a look at this website, there's lots of good information and an autorun.inf generator that you can play with.

Matthew Scharley
+3  A: 

A quick search revealed this site, see section "3.3 Device change listener"

martinsb
+2  A: 

You could have a background application reacting to the connect event of this particular USB device, this would start the actual application.

ManagementEventWatcher Watcher;
WqlEventQuery Query = new WqlEventQuery();
Query.EventClassName = "__InstanceCreationEvent";
Query.Condition = "TargetInstance ISA 'Win32_USBControllerDevice'";
Query.WithinInterval = new TimeSpan(0, 0, 2);

Watcher = new ManagementEventWatcher(Query);
Watcher.EventArrived += new EventArrivedEventHandler(OnUsbConnected);

The OnUsbConnected handler would then start the desired application.

Treb
+1  A: 

Monoxide has the right idea. I use this technique myself in managing my music collection. My main PC is a laptop, but my music collection got big enough that I had to move it to an external drive. So on the external drive I put the following AUTORUN.INF:

[autorun]
open=c:\progra~1\itunes\itunes.exe
label=Open iTunes
icon=c:\progra~1\itunes\itunes.exe,0

As you can see it offers to launch iTunes from C: when this drive is attached. For some reason the label and icon don't get picked up by the AutoPlay window, but LABEL does appear when this drive is viewed in My Computer. What you see in the AutoPlay dialog that comes up in XP is the default selection is "Run the program / using the program provided on the device". One click and you are off and running.

Tim Farley
No, you're right, it is inf, and i fixed my post. Nice catch.
Matthew Scharley