Getting connected USB info with Delphi on Vista

后端 未结 1 923
孤街浪徒
孤街浪徒 2021-01-01 04:35

How can I get \'connected usb info\'(device instance id, driver key name ..) from Registry in Vista or Windows 7 by using delphi? Where is this information in Windows Regist

相关标签:
1条回答
  • 2021-01-01 05:28

    You can use the WMI class Win32_DiskDrive. if you need get info about the logical drive you can query the wmi with something like this

    Select * Win32_LogicalDisk where DriveType = 2
    

    to access the WMI from delphi you must import the Microsoft WMIScripting V1.x Library using Component->Import Component->Import type library->Next->"Select the library"->Next->Add unit to project->Finish.

    if you need more info about usb devices you can check also the next classes

    • Win32_USBControllerDevice
    • Win32_PnPEntity

    See this example (tested in Delphi 2007 and Windows 7)

    program GetWMI_USBConnectedInfo;
    
    {$APPTYPE CONSOLE}
    
    uses
      Classes,
      ActiveX,
      Variants,
      SysUtils,
      WbemScripting_TLB in '..\..\..\Documents\RAD Studio\5.0\Imports\WbemScripting_TLB.pas';
    
    
    procedure  GetUSBDiskDriveInfo;
    var
      WMIServices : ISWbemServices;
      Root        : ISWbemObjectSet;
      Item        : Variant;
      i           : Integer;
      StrDeviceUSBName: String;
    begin
      WMIServices := CoSWbemLocator.Create.ConnectServer('.', 'root\cimv2','', '', '', '', 0, nil);
      Root  := WMIServices.ExecQuery('Select * From Win32_DiskDrive Where InterfaceType="USB"','WQL', 0, nil);//more info in http://msdn.microsoft.com/en-us/library/aa394132%28VS.85%29.aspx
      for i := 0 to Root.Count - 1 do
      begin
        Item := Root.ItemIndex(i);
        Writeln('Caption           '+VarToStr(Item.Caption));
        Writeln('DeviceID          '+VarToStr(Item.DeviceID));
        Writeln('FirmwareRevision  '+VarToStr(Item.FirmwareRevision));
        Writeln('Manufacturer      '+VarToStr(Item.Manufacturer));
        Writeln('Model             '+VarToStr(Item.Model));
        Writeln('PNPDeviceID       '+VarToStr(Item.PNPDeviceID));
        Writeln('Status            '+VarToStr(Item.Status));
      End;
    end;
    
    
    begin
      try
        CoInitialize(nil);
        GetUSBDiskDriveInfo;
        Readln;
        CoUninitialize;
      except
        on E:Exception do
        Begin
            CoUninitialize;
            Writeln(E.Classname, ': ', E.Message);
            Readln;
        End;
      end;
    end.
    
    0 讨论(0)
提交回复
热议问题