在Vista上使用Delphi连接USB信息 [英] Getting connected USB info with Delphi on Vista

查看:391
本文介绍了在Vista上使用Delphi连接USB信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过使用delphi从Vista或Windows 7的注册表中获取连接的usb信息(设备实例ID,驱动程序密钥名称..)?
Windows注册表中的这些信息在哪里?
我有一个代码,它在XP上工作但不在Vista中(c ++代码: http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx
为什么代码在Vista上不起作用?
我真的是堆叠了。请帮助。

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.(c++ code: http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx) Why the code is not working on Vista? I'm really stack about that. Please help.

非常感谢您的答案。

推荐答案

您可以使用 WMI 课程 Win32_DiskDrive 。如果您需要获取有关逻辑驱动器的信息,您可以使用这样的方式查询wmi。

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

访问 WMI 从delphi您必须导入Microsoft WMIScripting V1.x库使用组件 - >导入组件 - >导入类型库 - >下一步 - >选择库 - >下一步 - >添加单位到项目 - >完成。

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.

如果您需要有关usb设备的更多信息,您还可以检查下一个类

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

  • Win32_USBControllerDevice
  • Win32_PnPEntity

看到这个例子(在Delphi 2007中测试和Windows 7)

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.

这篇关于在Vista上使用Delphi连接USB信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆