开发者

WMI: How to distinguish between an internal "local disk" HDD and an external "local disk" HDD

开发者 https://www.devze.com 2022-12-21 22:14 出处:网络
Background I\'ve been using Win32_DiskDrive to find flash memory (usb pens, SD cards, etc.), but after some tests on other computers I noticed that they weren\'t always discovered. So I am using Win32

Background

I've been using Win32_DiskDrive to find flash memory (usb pens, SD cards, etc.), but after some tests on other computers I noticed that they weren't always discovered. So I am using Win32_LogicalDisk and since it has DriveType I don't have to associate with two classes (e.g. partition) to find first the drives then their drive letters.

The problem is that external harddrives are detected as Drive开发者_开发技巧Type 3 (Local Disk) in LogicalDisk and doesn't have 7 (Supports Removable Media) in Capabilities in DiskDrive. So I can't tell the difference between an internal and external drive.

Question

How do I tell the difference between an internal and an external harddrive using LogicalDisk (or DiskDrive if you really have to) or something third.

Alright. The question has been answered!

Here's the code, if anyone is interested.

program GetWMI_USBConnectedInfo;

{$APPTYPE CONSOLE}

uses
  Windows,
  Classes,
  ActiveX,
  Variants,
  SysUtils,
  WbemScripting_TLB, // Using the .pas supplied by the wrapper as it seems to be the XP version of 1.2
  magwmi,
  magsubs1;

function CheckType(Str: string): boolean;
var
  I: Integer;
  Str2: string;
begin
  Result := False;
  for I := 1 to Length(Str) - 1 do if Str[I] = '\' then begin
    Str2 := Copy(Str, 1, I-1);
    Str2 := LowerCase(Str2);
    if (Str2 = 'usbstor') or (Str2 = 'flashmedia') then
      Result := True;
    Break;
  end;
end;

procedure  GetUSBDiskDriveInfo;
var
  I, II, III:  Integer;
  Start, Stop, Freq: Int64;
  instances, instances2, instances3: integer ;
  WmiResults, WmiResults2, WmiResults3: T2DimStrArray ;
  errstr: string ;
begin
  QueryPerformanceFrequency(Freq);
  QueryPerformanceCounter(Start);
  try
    MagWmiGetInfoEx('.', 'root\CIMV2', '', '', 'SELECT * FROM Win32_DiskDrive', WmiResults, instances, errstr);
    for I := 1 to instances do begin
      MagWmiGetInfoEx('.', 'root\CIMV2', '', '', 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID=''' + WmiResults[I, 12] + '''} WHERE AssocClass = Win32_DiskDriveToDiskPartition', WmiResults2, instances2, errstr);
      for II := 1 to instances2 do begin
        MagWmiGetInfoEx('.', 'root\CIMV2', '', '', 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID=''' + WmiResults2[II, 11] + '''} WHERE AssocClass = Win32_LogicalDiskToPartition', WmiResults3, instances3, errstr);
        for III := 1 to instances3 do begin
          if CheckType(WmiResults[I, 32]) or (Pos('7', WmiResults[I, 3])>0) then begin
            Write(WmiResults3[III, 4]);
            Write(WmiResults3[III, 39]);
            Writeln;
          end;
        end;
        WmiResults3 := nil;
      end;
      WmiResults2 := nil;
    end;
    WmiResults := nil;
  except
    Writeln;
    Writeln('error: '+errstr);
  end;
  Writeln;
  QueryPerformanceCounter(Stop);
  if (Freq > 0) then
    Writeln('It took ' + FormatFloat('0.#0', (Stop-Start) / Freq) + ' seconds to complete.');
end;

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

One more thing!

Call this a dirty hack or whatever, but I commented out this part of MagWmiGetInfoEx (line 298 in magwmi) in order to make it work:

//        if Pos ('SELECT', Arg) = 1 then
            wmiObjectSet := wmiServices.ExecQuery (Arg, 'WQL', wbemFlagReturnImmediately, nil)
//        else
//            wmiObjectSet := wmiServices.InstancesOf (Arg, wbemFlagReturnImmediately or
//                                                             wbemQueryFlagShallow, nil)
;


I would suggest sticking with WMI. There is a good delphi wrapper available which includes full source to get you started.

A query to get you started is "SELECT * FROM WIN32_DiskDrive" which would return all of the information for all of the disk drives in your system. the PNPDeviceID field should start with USBSTOR for any USB drives. A good resource for what fields come back is the MSDN website. Just translate the objects into queries.

If your going to be calling this from a thread, you may need to add initialize COM (ComInitialize) before making any calls. Before destroying your thread, call ComUnitialialize.


You can test this package; GLibWMI Components Library in SourceForge. It's a wrapper for work with WMI. Include components like CDiskDriveInfo, CDiskPartitionInfo, CUSBControllerInfo,... that can help you.

Additionally all the code is included. You can evaluate it.

Regards.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号