admin

[系统应用] 读取USB设备特征码,读取其他硬件信息

unit Unit1;


interface


uses

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

  Dialogs, ExtCtrls,ActiveX, ComObj, StdCtrls;


type

  TForm1 = class(TForm)

    Button2: TButton;

    Memo1: TMemo;

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


{$R *.dfm}


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

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


end;

end; 


end.







#1楼
发帖时间:2017-6-11   |   查看数:0   |   回复数:1
admin

function GetWMIProperty(WMIType, WMIProperty: string): string;

var Wmi, Objs, Obj: OleVariant;

  C: Cardinal;

  i,Len:integer;

  tempItem:IEnumVariant;

begin

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

  Objs := Wmi.ConnectServer('.','root\cimv2').ExecQuery('Select * from Win32_' + WMIType);

  tempItem:=IEnumVariant(IUnknown(Objs._NewEnum));

  Result:='';

  while (tempItem.Next(1, obj, c) = S_OK) do

  begin

    Obj := Obj.Properties_.Item(WMIProperty, 0).Value;

    if not VarIsNull(obj) then

    begin

      Result :=Result +trim(Obj);

      break;

    end;

  end;

end;




因为WMI中通用的类大多是Win32_开头的,这里只需要传递后面的部分。


那么下一步,怎么知道我需要的是什么参数呢?以获取计算机名为例:


str := GetWMIProperty('OperatingSystem','CSName');即可获得。


当然,计算机名信息是比较常用的,在好几个类里面都有。


Win32_开头的类有:


Win32_Account

Win32_ActiveRoute

Win32_AutochkSetting

Win32_BaseBoard

Win32_BaseService

Win32_BIOS

Win32_BootConfiguration

Win32_Bus

Win32_ComputerSystem

Win32_ComputerSystemWindowsProductActivation

Win32_Desktop

Win32_DesktopMonitor

Win32_DeviceSettings

Win32_DiskDrive

Win32_DiskDrivePhysicalMedia

Win32_DisplayConfiguration

Win32_Environment

Win32_FontIntoAction

Win32_Group

Win32_IDEController

Win32_IP4PersistedRouteTable

Win32_IP4RouteTable

Win32_Keyboard

Win32_LogicalDisk

Win32_LogicalProgramGroup

Win32_LogonSession

Win32_MappedLogicalDisk

Win32_MemoryDevice

Win32_MotherboardDevice

Win32_NetworkAdapter

Win32_NetworkAdapterConfiguration

Win32_NetworkAdapterSetting

Win32_NetworkClient

Win32_NetworkConnection

Win32_OperatingSystem

Win32_PerfFormattedData_RemoteAccess_RASPort

Win32_PerfFormattedData_RemoteAccess_RASTotal

Win32_PerfRawData_RemoteAccess_RASPort

Win32_PhysicalMedia

Win32_PhysicalMemory

Win32_PNPDevice

Win32_PortConnector

Win32_POTSModem

Win32_POTSModemToSerialPort

Win32_Printer

Win32_PrinterConfiguration

Win32_Process

Win32_Processor

Win32_Product

Win32_ProgramGroup

Win32_ScheduledJob

Win32_SerialPort

Win32_SerialPortConfiguration

Win32_SerialPortSetting

Win32_Service

Win32_ServiceControl

Win32_SoundDevice

Win32_StartupCommand

Win32_SystemAccount

Win32_SystemBIOS

Win32_SystemSetting

Win32_SystemTimeZone

Win32_SystemUsers

Win32_TapeDrive

Win32_TemperatureProbe 

Win32_USBController

Win32_USBControllerDevice

Win32_UserAccount

Win32_VideoController

Win32_VideoSettings

Win32_WindowsProductActivation

 


首先,来总结一下Win32_OperatingSystem,这个类里面常用的属性有:


caption:操作系统名称(windows xp professional)。

CountryCode:所在地区的区码(86)。

CSName:计算机名。

SerialNumber:操作系统注册的序列号。

TotalVisibleMemorySize:电脑物理内存(Kb)。

Version:windows版本(5.1.2600)。

windows版本号和版本的对应,看这里。

 

下一个,Win32_Bios,包含硬件相关属性:

Manufacturer:制造商(LENOVO)

ReleaseDate:出厂日期。

 

Win32_PhysicalMedia:

SerialNumber:硬盘序列号。

  

类和属性都不区分大小写。


2017-6-11 #2楼
游客组