1. 程式人生 > >Delphi通過WMI獲取系統資訊

Delphi通過WMI獲取系統資訊

uses ActiveX, ComObj;

function GetWMIProperty(WMIType, WMIProperty: string): string;
var
  Wmi, Objs, Obj: OleVariant;
  Enum: IEnumVariant;
  C: Cardinal;
begin
  Wmi:= CreateOleObject('WbemScripting.SWbemLocator');
  Objs := Wmi.ConnectServer('.','root/cimv2').ExecQuery('Select * from Win32_' + WMIType);
  Enum := IEnumVariant(IUnknown(Objs._NewEnum));
  Enum.Reset;
  Enum.Next(1, Obj, C);
  Obj := Obj.Properties_.Item(WMIProperty, 0).Value;
  Result := Obj;
end;
 

// 獲取硬碟序列號
ShowMessage(GetWMIProperty('DiskDrive', 'PNPDeviceID'));
// 獲取BISO序列號
ShowMessage(GetWMIProperty('BIOS', 'SerialNumber'));
// 獲取網絡卡MAC地址
ShowMessage(GetWMIProperty('NetworkAdapter', 'MACAddress'));
// 獲取網絡卡序列號
ShowMessage(GetWMIProperty('NetworkAdapter', 'PNPDeviceID'));
// 獲取CPU序列號
ShowMessage(GetWMIProperty('Processor', 'ProcessorId'));

也可以用同樣的方法獲得任意感興趣的系統資訊,比如正在執行的程序、賬戶資訊等等。

更多WMI的資訊參考:
http://www.microsoft.com/whdc/system/pnppwr/wmi/default.mspx
http://msdn2.microsoft.com/en-us/library/aa394572.aspx