WMI: how to distinguish an internal hard drive of a local drive from an external hard drive of a local drive

Background
I used Win32_DiskDrive to search for flash memory (USB sticks, SD cards, etc.), but after some tests on other computers I noticed that they were not always detected. Therefore, I use Win32_LogicalDisk, and since it has DriveType, I do not need to bind to two classes (for example, a partition) to find the drives first, and then their drive letters.

The problem is that external hard drives are detected as DriveType 3 (local disk) in LogicalDisk and do not have 7 (supports removable media) in DiskDrive capabilities. Therefore, I can not tell the difference between the internal and external drives.

Question
How to find out the difference between an internal and external hard drive using LogicalDisk (or DiskDrive, if you really need to) or something else.

Good. The answer has been given!
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 it a dirty hack or something else, but I commented on this part of MagWmiGetInfoEx (line 298 in magwmi) 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) ; 
+4
source share
2 answers

I suggest stick with WMI. There is a nice delphi wrapper that includes the complete source to get you started.

The query to run is: "SELECT * FROM WIN32_DiskDrive", which returns all the information for all drives in your system. The PNPDeviceID field must begin with USBSTOR for any USB drives. A good resource for field returns is MSDN . Just translate objects into queries.

If you are going to call this from a stream, you may need to add COM (ComInitialize) initialization before making any calls. Before destroying the stream, call ComUnitialialize.

+4
source

You can test this package; GLibWMI Component Library at SourceForge. This is a shell for working with WMI. Include components such as CDiskDriveInfo, CDiskPartitionInfo, CUSBControllerInfo ... that may help you.

In addition, all code is included. You can rate it.

Sincerely.

+1
source

Source: https://habr.com/ru/post/1302594/


All Articles