wpfwannabe
wpfwannabe

Reputation: 14877

Get specific device type from a drive letter

I want to detect when a SD (or other card) is inserted into the card reader. I know about WM_DEVICECHANGE but I need to get specific device type information given the drive letter. For instance, I want to distinguish between any card in the card reader from an "ordinary" flash drive. GetDriveType() doesn't seem to be refined enough. What are my other choices?

I am happy with a Windows 7 solution. I do not need backward compatibility.

Is this type of refinement possible all?

I have found this topic which doesn't provide a satisfactory answer. This one feels like the way to go but there is no sample code or pointers to sample code. It's unbelievable that nobody has pieced together a working sample for such a common request.

[EDIT]

I have also found this. The sample provided has a few errors but after fixing it I still get no result. For a device which has a SD card inserted I get a BusTypeUnknown instead of BusTypeSd in pDeviceDesc.BusType. This seemed straightforward and still failed.

Upvotes: 17

Views: 1602

Answers (2)

Cees Timmerman
Cees Timmerman

Reputation: 19644

This looks useful: How Can I Determine Which USB Devices are Connected to a Computer?

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDevices = objWMIService.ExecQuery _
    ("Select * From Win32_USBControllerDevice")

For Each objDevice in colDevices
    strDeviceName = objDevice.Dependent
    strQuotes = Chr(34)
    strDeviceName = Replace(strDeviceName, strQuotes, "")
    arrDeviceNames = Split(strDeviceName, "=")
    strDeviceName = arrDeviceNames(1)
    Set colUSBDevices = objWMIService.ExecQuery _
        ("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
    For Each objUSBDevice in colUSBDevices
        Wscript.Echo objUSBDevice.Description
    Next    
Next

Sample output (should be a tree):

USB Root Hub
Microsoft USB IntelliMouse Web
Microsoft USB IntelliMouse Web
USB Mass Storage Device
Disk drive
Generic volume
USB Root Hub
USB Root Hub

I hope not all USB storage describes itself as "USB Mass Storage Device". If it does, check the volume name or autorun.inf, if any.

Microsoft being among the inventors of USB, it's unlikely that you'll need this Linux info, but it could be harvested for search terms like "Pendrive" and "Flash Drive".

Upvotes: 1

Simon
Simon

Reputation: 1851

I don't think it's possible since card readers present their storage in the same way as a regular flash drive to the OS. They're both USB mass storage class devices. Here's the Wikipedia article on this device class. The OS has no way of distinguishing between those two device types.

Upvotes: 0

Related Questions