且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取USB设备的驱动器号?

更新时间:2022-01-16 01:30:23

如果你正在获得价值像 \\.\PHYSICALDRIVE1 表示您正在使用 Win32_DiskDrive wmi类和 DeviceID 属性,所以为了获取驱动器盘符,您必须使用ASSOCIATORS类,这将在wmi类之间创建一个链接,其中包含您要查找的信息( Win32_LogicalDisk )和你所在的课程正在使用( Win32_DiskDrive )。

If you are getting values like \\.\PHYSICALDRIVE1 means which you are using the Win32_DiskDrive wmi class and the DeviceID Property , so in order to get the Drive letter you must use an ASSOCIATORS class, which will create a link between the wmi classes with contain the information which you are looking for (Win32_LogicalDisk) and the class which you are using (Win32_DiskDrive).

所以你不要这样做

Win32_DiskDrive - > Win32_DiskDriveToDiskPartition - > Win32_DiskPartition - > Win32_LogicalDiskToPartition - > Win32_LogicalDisk

Win32_DiskDrive-> Win32_DiskDriveToDiskPartition -> Win32_DiskPartition -> Win32_LogicalDiskToPartition -> Win32_LogicalDisk

查看此示例函数

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;


function DeviceIDToDrive(const ADeviceID : string) : string;
var
  FSWbemLocator  : OLEVariant;
  objWMIService  : OLEVariant;
  colLogicalDisks: OLEVariant;
  colPartitions  : OLEVariant;
  objPartition   : OLEVariant;
  objLogicalDisk : OLEVariant;
  oEnumPartition : IEnumvariant;
  oEnumLogical   : IEnumvariant;
  iValue         : LongWord;
  DeviceID       : string;
begin;
  Result:='';
  FSWbemLocator   := CreateOleObject('WbemScripting.SWbemLocator');
  objWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  DeviceID        := StringReplace(ADeviceID,'\','\\',[rfReplaceAll]); //Escape the `\` chars in the DeviceID value because the '\' is a reserved character in WMI.
  colPartitions   := objWMIService.ExecQuery(Format('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="%s"} WHERE AssocClass = Win32_DiskDriveToDiskPartition',[DeviceID]));//link the Win32_DiskDrive class with the Win32_DiskDriveToDiskPartition class
  oEnumPartition  := IUnknown(colPartitions._NewEnum) as IEnumVariant;
  while oEnumPartition.Next(1, objPartition, iValue) = 0 do
   begin
       if not VarIsNull(objPartition.DeviceID) then
       begin
        colLogicalDisks := objWMIService.ExecQuery('ASSOCIATORS OF {Win32_DiskPartition.DeviceID="'+VarToStr(objPartition.DeviceID)+'"} WHERE AssocClass = Win32_LogicalDiskToPartition'); //link the Win32_DiskPartition class with theWin32_LogicalDiskToPartition class.
        oEnumLogical  := IUnknown(colLogicalDisks._NewEnum) as IEnumVariant;
          if oEnumLogical.Next(1, objLogicalDisk, iValue) = 0 then
          begin
              Result:=objLogicalDisk.DeviceID;
              objLogicalDisk:=Unassigned;
          end;
       end;
       objPartition:=Unassigned;
   end;
end;

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