[python-win32] USB Insertion Extrinsic Event exists?
Tim Golden
mail at timgolden.me.uk
Sat Feb 13 13:32:50 CET 2010
On 12/02/2010 16:58, Chris Jesse wrote:
> So it turns out that my description of the task was lacking in one
> simple, but critical, observation. We use removable media, usually a compact
> flash card, plugged into a USB adapter drive. The events get triggered for
> my pen drive, but when the compact flash card is inserted I get nothing -
> probably because the device is already plugged in.
OK; just tried it and I see what you mean. In my case, I'm using
one of those card readers with four different-sized slots. It
"pre-registers" as four drives so you don't get any further device
change events. (I assume this is comparable to what you're doing...)
All I can suggest is that you watch the Win32_DiskDrive instances
with an InterfaceType of "USB" for a modification event and check
whether the MediaLoaded has changed. Something like this:
<code>
import wmi
c = wmi.WMI ()
watcher = c.Win32_DiskDrive.watch_for (InterfaceType="USB")
while True:
try:
disk = watcher (timeout_ms=10000)
print disk
except wmi.x_wmi_timed_out:
pass
</code>
If you know which device id will always be firing (eg the only
slot I can use is PHYSICALDEVICE3 then you can build that into
the query.
> There's an interesting document attached which shows some potential
>C++ code; but I'm in a little out of my depth here - "help"!
About half of that is basically the code I posted here:
http://local.timgolden.me.uk/python/win32_how_do_i/detect-device-insertion.html
The other half deals with the case where you want to use this
sort of technique but where you're in a service or otherwise
don't have a top-level window. If the WMI stuff doesn't work
for you and you need this kind of thing (and don't have/want
a top-level window) then we can try to run something up on
the basis of the article.
TJG
More information about the python-win32
mailing list