Retrieving USB Information from Delphi on Vista

How can I get "usb related information" (device instance id, driver key name ..) from the registry in Vista or Windows 7 using delphi? Where is this information in the windows registry? I have code that runs on XP, but not on Vista. (C ++ code: http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx ) Why does the code not work with Vista? I really stop about it. Please, help.

Thanks so much for your answers.

+5
source share
1 answer

You can use the Win32_DiskDrive WMI class , if you need logical drive information, you can request wmi with something like this

Select * Win32_LogicalDisk where DriveType = 2

To access WMI from delphi, you must import the Microsoft WMIScripting V1.x library using Component-> Import Component-> Import type library → Next → “Select Library” → Next-> Add Block to Project-> Done.

if you need more information about USB devices, you can also check the following classes

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.
+10
source

All Articles