Delphi调用WMI读取USB设备的PID和VID

以下代码出自http://blog.csdn.net/hellogv/,引用请注明出处!


WMI(Windows®Management Instrumentation )非常强大,它可以以数据库的形式查询你的电脑的软件和硬件,在它的数据库里面,时刻保存着最新的软件信息和硬件信息,因此你可以用WMI来检测CUP主频、温度,读取WINDOWS的进程.........

本文就介绍如何使用WMI读取USB设备的PID和VID码

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls,ActiveX, ComObj;



procedure TForm1.Button2Click(Sender: TObject);

var

Locator: OleVariant;

WMI: OleVariant;

RET: OleVariant;

Enum: IEnumVariant;

Tmp: OleVariant;

Value: Cardinal;

begin;

Locator := CreateOleObject('WbemScripting.SWbemLocator');

WMI := Locator.ConnectServer('.', '', '', '');

Ret := WMI.ExecQuery('SELECT * FROM Win32_USBHub');//这里Win32_USBHub为检测项,详见下表



Enum:= IUnknown(RET._NewEnum) as IEnumVariant;

while (Enum.Next(1, Tmp, Value) = S_OK) do

begin

  if Tmp.Name='USB Mass Storage Device' then

     ShowMessage('这个u盘的硬件特征码是'+Tmp.PNPDeviceID);//Tmp为检测到返回的数据集合,其中PNPDeviceID为集合中的对象,包含了PID和VID码

end;



end;



Win32_USBHub只是冰山一角,常用的还有以下



  "Win32_1394Controller"                ,

  "Win32_BaseBoard"                     ,

  "Win32_Battery"                       ,

  "Win32_BIOS"                          ,

  "Win32_Bus"                           ,

  "Win32_CacheMemory"                   ,

  "Win32_CDROMDrive"                    ,

  "Win32_CurrentProbe"                  ,

  "Win32_DesktopMonitor"                ,

  "Win32_DeviceMemoryAddress"           ,

  "Win32_DiskDrive"                     ,

  "Win32_DisplayConfiguration"          ,

  "Win32_DisplayControllerConfiguration",

  "Win32_DMAChannel"                    ,

  "Win32_Fan"                           ,

  "Win32_FloppyController"              ,

  "Win32_FloppyDrive"                   ,

  "Win32_HeatPipe"                      ,

  "Win32_IDEController"                 ,

  "Win32_InfraredDevice"                ,

  "Win32_IRQResource"                   ,

  "Win32_Keyboard"                      ,

  "Win32_MemoryArray"                   ,

  "Win32_MemoryDevice"                  ,

  "Win32_MotherboardDevice"             ,

  "Win32_NetworkAdapter"                ,

  "Win32_NetworkAdapterConfiguration"   ,

  "Win32_OnBoardDevice"                 ,

  "Win32_ParallelPort"                  ,

  "Win32_PCMCIAController"              ,

  "Win32_PhysicalMemory"                ,

  "Win32_PhysicalMemoryArray"           ,

  "Win32_PnPEntity"                     ,

  "Win32_PointingDevice"                ,

  "Win32_PortableBattery"               ,

  "Win32_PortConnector"                 ,

  "Win32_PortResource"                  ,

  "Win32_POTSModem"                     ,

  "Win32_PowerManagementEvent"          ,

  "Win32_Printer"                       ,

  "Win32_PrinterConfiguration"          ,

  "Win32_PrintJob"                      ,

  "Win32_Processor"                     ,

  "Win32_Refrigeration"                 ,

  "Win32_SerialPort"                    ,

  "Win32_SerialPortConfiguration"       ,

  "Win32_SMBIOSMemory"                  ,

  "Win32_SoundDevice"                   ,

  "Win32_SystemEnclosure"               ,

  "Win32_SystemMemoryResource"          ,

  "Win32_SystemSlot"                    ,

  "Win32_TapeDrive"                     ,

  "Win32_TemperatureProbe"              ,

  "Win32_UninterruptiblePowerSupply"    ,

  "Win32_USBController"                 ,

  "Win32_VideoConfiguration"            ,

  "Win32_VideoController"               ,

  "Win32_VoltageProbe"                  ,

你可能感兴趣的:(windows,.net,Blog,Delphi)