[python-win32] USB Insertion Extrinsic Event exists?

Chris Jesse chris.jesse at flightdataservices.com
Fri Feb 12 16:52:27 CET 2010

Hi TJG, 

Thanks for your very helpful reply and code snippet. I'm happy that there seems to be a way to do this in windows. The application is multi-threaded with the MainThread being a wxPython MainLoop which monitors for events such as Shutdown (QUERY_END_SESSION) etc. It has to be the MainThread in order to receive the events. 

Is it possible that the Win32_VolumeChangeEvent or Win32_DeviceChangeEvent events can be detected a seperate thread? The example "detect-device-insertion" uses an infinate loop win32gui.PumpMessages() for WM_DEVICECHANGE. We can't use this as our MainThread as it's already in the wx MainLoop(). 

Operating system wise, we have to support Ubuntu (hense the DeviceKit), Windows XP, Vista and 7. Scanning the list of devices isn't such a problem as we do it already; but of course the less we have to do the better! 

Thanks everyone for your help. 

Chris Jesse 
Software Architect 

Flight Data Services 
189-199 West Street, Fareham, PO16 0EN, UK 
Tel: +44 (0) 1329 223663 
Fax: +44 (0) 1329 223664 
Registered in England, No 4041206, Address: 14 Brookmeadow, Fareham, PO15 5JH 

----- Original Message ----- 
From: "Tim Golden" <mail at timgolden.me.uk> 
To: python-win32 at python.org 
Sent: Friday, 12 February, 2010 08:56:44 GMT +00:00 GMT Britain, Ireland, Portugal 
Subject: Re: [python-win32] USB Insertion Extrinsic Event exists? 

On 11/02/2010 18:18, Chris Jesse wrote: 
> Hi All, 
> I currently have a little program which looks for new USB removable media 
> to be inserted into a PC. It does so by polling (every 5 seconds) all 
> the drives within Win32_DiskDrive() and queries to find the ones which 
> PNPDeviceID has 'USBSTOR' within them and establishes that they are removable media 
> (I'm not interested in USB HDDs) and whether they have a partition or not. 

You've got two, perhaps three approaches you could take here. One 
is to use the WM_DEVICECHANGE windows message. I thought I had an 
example in my list of How-Tos but I see that I don't. Here's an old 
post illustratating a solution: 


Using WMI you could look at either the Win32_VolumeChangeEvent or 
the Win32_DeviceChangeEvent. Both are extrinsic so are basically 
proxying the WM_DEVICECHANGE above; the first is a subclass of the second. 

import wmi 
c = wmi.WMI () 

print c.Win32_DeviceChangeEvent 
# schema info shows that it relies on WM_DEVICECHANGE 

watcher = c.Win32_DeviceChangeEvent () 
event = watcher () 

# inserts USB stick 

print event 


However, on my WinXP box I'm not getting much information beyond 
the fact of the event; not sure if that's because of some flaw 
in my code or merely because XP doesn't support very much more 
than that. I'll try to run some tests. At the worst, you'd only 
need to scan the list of devices / volumes when an event occurs... 

python-win32 mailing list 
python-win32 at python.org 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100212/ee851972/attachment.htm>

More information about the python-win32 mailing list