views:

107

answers:

3

I want to detect when a Disk ( CD / DVD ) or an USB inserted or removed.

I know that I should work with WM_DEVICECHANGE message , but my problem is getting the Volume ( Letter ) of the Drive , I tried to Get it by WMI Query and Win32_VolumeChangeEvent too but I could not!

there is an Example and work fine , how can I do this by Delphi?

A: 

I haven't compiled and tested now, but a while ago I remember using this code to implement something similar. Hope it helps.

Erkan Haspulat
thanks , but my problem is getting the Drive letter , when an USB Inserted , first windows send an WM_DEVICECHANGE message to top-level Windows , but after sending message , windows make a new Drive for USB inserted and give it a name ( Volume ) , i want to get the new Volume ( letter ) created for the USB Drive , for example : " J:\ " ...
Mahmood_N
+1  A: 

Detecting Media Insertion or Removal (MSDN)
Detecting Hardware Insertion and/or Removal (codeproject.com)

// EDIT:

...get notified when a CD is removed or inserted? (Torry's Delphi Page)

and also

http://www.google.com/search?q=Delphi+WMDeviceChange

adf88
Nice, but not in delphi. However that should be enough to figure it out. Also interesting topic.
Bart
Yes, it's not Delphi. But it is not a C#! C++ and Objective Pascal are very close languages, translation will involve mainly some syntax correction and fit into Delphi's VCL framework.
adf88
@Bart: The Windows API looks the same in every programming language. If one knows a language (e.g. Delphi), one knows how to use external APIs in that language (Delphi).
Andreas Rejbrand
I found some Delphi code.
adf88
@Andreas: I had this in my mind when I wrote that this should be enough. Also +1 to author now.
Bart
A: 

For CD insertions, you want CDEvents: http://www.torry.net/vcl/filedrv/cd/dtcdevents.zip

Works beautifully. E-mail me ([email protected]) if you want sample code.

Erik Knowles