tags:

views:

483

answers:

1

Hello,

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 Registry? I have a code it's working on XP but not in Vista.(code: http://www.delphipraxis.net/post991546.html) Why the code is not working on Vista? I'm really stack about that. Please help.

Thanks a lot for your answers. [email protected]

+8  A: 

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

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.
RRUZ
@RRUZ: This is highly useful. I see in my IDE (Delphi 7, Windows 7) that this Active X control is not listed. Do I need a special SDK? Thanks, Brian.
Brian Frost
@Brian, you must check in the option "Import type library" (not activex) and search in the list "Microsoft WMI Scripting V1.2 Library"
RRUZ
This is really useful. Thanks a lot.