Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get specific device type from a drive letter

Tags:

winapi

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.

like image 408
wpfwannabe Avatar asked Feb 12 '12 12:02

wpfwannabe


1 Answers

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".

like image 72
Cees Timmerman Avatar answered Oct 02 '22 11:10

Cees Timmerman