4

I am trying to figure out how to detect if a drive is a mass storage device. I can get the drive letters but cannot figure out how to detect what sort of device it is. I am trying to detect if a Garmin GPS receiver is connected to a PC in mass storage mode.

1
  • The GPS device is connected via USB?
    – RRUZ
    Mar 2, 2012 at 5:58

1 Answer 1

5

You can use the WMI for this task, exist several classes which can help you to determine that information, start with the Win32_USBHub checking for the value Mass Storage Device in the the property Description, also take a look in the Win32_PNPEntity class. If you don't have experience accesing the WMI from Delphi, try using the Wmi delphi code creator

UPDATE

To associate the values returned by the Win32_USBHub or Win32_USBControllerDevice WMI classes with a Disk Drive letter, you must follow the next steps

  1. Query for the Win32_USBControllerDevice class.
  2. Using the DeviceID extracted of the dependent property of each returned instance(record) check the Win32_PnPEntity class loinked to a Win32_DiskDrive using a WQL sentence like so : ASSOCIATORS OF {Win32_PnPEntity.DeviceID="DeviceID"} WHERE ResultClass = Win32_DiskDrive
  3. Now using Win32_DiskDriveToDiskPartition class you can found the link between the Disk Drive and the partition.
  4. Finally using the Win32_LogicalDiskToPartition class you can extract the Drive letter.

Check this sample code

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Types,
  StrUtils,
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

procedure ScanUSBPnpDevices;
const
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator  : OLEVariant;
  objWMIService  : OLEVariant;

  USBControllerDevices: OLEVariant;
  USBControllerDevice : OLEVariant;
  EnumUSBDevice       : IEnumvariant;

  PnPEntities         : OLEVariant;
  PnPEntity           : OLEVariant;
  EnumPnPEntity       : IEnumvariant;

  DiskDrives          : OLEVariant;
  DiskDrive           : OLEVariant;
  EnumDiskDrive       : IEnumvariant;

  DiskPartitions      : OLEVariant;
  DiskPartition       : OLEVariant;
  EnumDiskPartition   : IEnumvariant;

  iValue         : LongWord;
  DeviceID       : string;
  DiskDeviceID   : string;
  DiskPartID     : string;

  StringDynArray : TStringDynArray;
begin;
  FSWbemLocator   := CreateOleObject('WbemScripting.SWbemLocator');
  objWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  //This will scan all the usb device, you can filter this WQL sentece using any property of this class, to speed-up the process.
  USBControllerDevices := objWMIService.ExecQuery('SELECT Dependent FROM Win32_USBControllerDevice','WQL',wbemFlagForwardOnly);
  EnumUSBDEvice        := IUnknown(USBControllerDevices._NewEnum) as IEnumVariant;
  while EnumUSBDEvice.Next(1, USBControllerDevice, iValue) = 0 do
  begin
    StringDynArray:=SplitString(USBControllerDevice.Dependent, '=');
    DeviceID:=StringDynArray[1];
    Writeln(Format('USB Controller Device Device ID %s',[DeviceID]));
    PnPEntities     := objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_PnPEntity.DeviceID=%s} WHERE ResultClass = Win32_DiskDrive',[DeviceID]),'WQL',wbemFlagForwardOnly);
    EnumPnPEntity   := IUnknown(PnPEntities._NewEnum) as IEnumVariant;
    while EnumPnPEntity.Next(1, PnPEntity, iValue) = 0 do
    begin
      //Escape the `\` chars in the DeviceID value because the '\' is a reserved character in WMI.
      DiskDeviceID   := PnPEntity.DeviceId;
      Writeln(Format('  Disk Drive Device ID %s',[DiskDeviceID]));
      DiskDeviceID   := StringReplace(DiskDeviceID,'\','\\',[rfReplaceAll]);;
      DiskDrives     := objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="%s"} WHERE AssocClass = Win32_DiskDriveToDiskPartition',[DiskDeviceID]),'WQL',wbemFlagForwardOnly);
      EnumDiskDrive  := IUnknown(DiskDrives._NewEnum) as IEnumVariant;
      while EnumDiskDrive.Next(1, DiskDrive, iValue) = 0 do
      begin
        DiskPartID:=DiskDrive.deviceID;
        Writeln(Format('    Disk Partition ID %s',[DiskPartID]));
        DiskPartitions:=objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_DiskPartition.DeviceID="%s"} WHERE AssocClass = Win32_LogicalDiskToPartition',[DiskPartID]),'WQL',wbemFlagForwardOnly);
        EnumDiskPartition  := IUnknown(DiskPartitions._NewEnum) as IEnumVariant;
        while EnumDiskPartition.Next(1, DiskPartition, iValue) = 0 do
        begin
           Writeln(Format('      Drive Letter %s',[String(DiskPartition.DeviceID)]));
           DiskPartition:=Unassigned;
        end;
        DiskDrive:=Unassigned;
      end;
      PnPEntity:=Unassigned;
    end;
    USBControllerDevice:=Unassigned;
  end;
end;

begin
 try
    CoInitialize(nil);
    try
      ScanUSBPnpDevices;
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
  Readln;
end.

Which will return some thing like this.

USB Controller Device Device ID "USBSTOR\\DISK&VEN_HP&PROD_V100W&REV_1.00\\3S980
62800DD&0"
  Disk Drive Device ID \\.\PHYSICALDRIVE1
    Disk Partition ID Disk #1, Partition #0
      Drive Letter F:
3
  • thanks for the info I have had a quick look and it does look look I should be able to find the device using WIN_Entity, however how do I then relate that back to a drive letter. I presume I could do this with the PNPDeviceID? thanks Colin
    – colin
    Mar 2, 2012 at 12:33
  • thanks for the example, very impressive, when my PC executes the line while EnumPnPEntity.Next(1, PnPEntity, iValue) = 0 do there seems to be quite a delay (about 10 seconds) before moving to the next line of code, is there a reason for this. Thanks Colin
    – colin
    Mar 3, 2012 at 10:42
  • I just update the code adding a Forward Only enumerator, this must improve the performance.
    – RRUZ
    Mar 3, 2012 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.