晕!这个不是很简单的东西吗? //---------WMI---------- type Rec_Wmi = record ComputerName: string; Namespace: string; User: string; Password: string; WMIType: string; Enum: IEnumVariant; class function GetWmiEnum(WMIType: string; var AEnum: IEnumVariant; Namespace: string = 'root\cimv2'; Where: string = ''; ComputerName: string = ''; User: string = ''; Password: string = ''): string; static; class function GetWMIProperty(Enum: IEnumVariant; WMIProperty: string; Index: integer = 0): OleVariant; static; class function SetWMIProperty(Enum: IEnumVariant; WMIProperty: string; value: string; Index: integer = 0): string; static; function GetEnum(Where: string = ''): string; function GetProperty(WMIProperty: string; Index: integer = 0): OleVariant; function SetProperty(WMIProperty: string; value: string): string; end;
{ Rec_Wmi }
function Rec_Wmi.GetEnum(Where: string): string; begin if Self.ComputerName = '' then Self.ComputerName := '.'; if Self.Namespace = '' then Self.Namespace := 'root\cimv2'; Result := GetWmiEnum(Self.WMIType, Self.Enum, Self.Namespace, Where, Self.ComputerName, Self.User, Self.Password); end;
function Rec_Wmi.GetProperty(WMIProperty: string; Index: integer): OleVariant; begin Result := TrueStr; if VarIsNull(Self.Enum) then Result := Self.GetEnum; if Result <> TrueStr then exit(''); Result := Self.GetWMIProperty(Self.Enum, WMIProperty, Index); end;
class function Rec_Wmi.GetWmiEnum(WMIType: string; var AEnum: IEnumVariant; Namespace, Where, ComputerName, User, Password: string): string; var Wmi, Objs : OleVariant; sql: string; begin Result := FalseStr; try if Where = '' then sql := 'Select * from ' + WMIType else sql := 'Select * from ' + WMIType + ' where ' + Where; if ComputerName = '' then ComputerName := '.'; Wmi := CreateOleObject('WbemScripting.SWbemLocator'); if (User <> '') and (Password <> '') then Objs := Wmi.ConnectServer(ComputerName, Namespace, User, Password).ExecQuery(sql) else Objs := Wmi.ConnectServer(ComputerName, Namespace).ExecQuery(sql); if VarIsNull(Objs) then exit('未找到 ' + WMIType); AEnum := IEnumVariant(IUnknown(Objs._NewEnum)); Result := TrueStr; except on E: Exception do Result := E.Message; end;
end;
class function Rec_Wmi.GetWMIProperty(Enum: IEnumVariant; WMIProperty: string; Index: integer): OleVariant; var obj: OleVariant; C: Cardinal; i: integer; hr: HResult; begin Result := null; try Enum.Reset; i := 0; while i <= index do begin hr := Enum.Next(1, obj, C); inc(i); end; if (hr = S_OK) then Result := obj.Properties_.Item(WMIProperty, 0).value; except on E: Exception do Result := ''; end; end;
function Rec_Wmi.SetProperty(WMIProperty, value: string): string; begin Result := TrueStr; if VarIsNull(Self.Enum) then Result := Self.GetEnum; Result := Self.SetWMIProperty(Self.Enum, WMIProperty, value);
end;
class function Rec_Wmi.SetWMIProperty(Enum: IEnumVariant; WMIProperty, value: string; Index: integer): string; var obj: OleVariant; C: Cardinal; i: integer; hr: HResult; begin Result := FalseStr; try Enum.Reset; i := 0; while i <= Index do begin hr := Enum.Next(1, obj, C); inc(i); end; if (hr = S_OK) then begin obj.Properties_.Item(WMIProperty, 0).value := value; obj.Put_; end; Result := TrueStr; except on E: Exception do Result := E.Message; end;
Memo_Info.Lines.Add('-----BIOS--------'); Awmi.WMIType := 'Win32_BIOS'; Memo_Info.Lines.Add('GetEnum:' + Awmi.GetEnum); Memo_Info.Lines.Add('Win32_BIOS SerialNumber :' + vartostr(Awmi.GetProperty('SerialNumber'))); Memo_Info.Lines.Add('Win32_BIOS TargetOperatingSystem :' + vartostr(vartostr(Awmi.GetProperty('TargetOperatingSystem')))); Memo_Info.Lines.Add('Win32_BIOS Manufacturer :' + vartostr(Awmi.GetProperty('Manufacturer'))); Memo_Info.Lines.Add('Win32_BIOS BuildNumber :' + vartostr(Awmi.GetProperty('BuildNumber'))); Memo_Info.Lines.Add('Win32_BIOS InstallDate :' + vartostr(Awmi.GetProperty('InstallDate'))); Memo_Info.Lines.Add('Win32_BIOS ReleaseDate :' + vartostr(Awmi.GetProperty('ReleaseDate'))); Memo_Info.Lines.Add('Win32_BIOS Version :' + vartostr(Awmi.GetProperty('Version'))); Memo_Info.Lines.Add('Win32_BIOS CodeSet :' + vartostr(Awmi.GetProperty('CodeSet'))); Memo_Info.Lines.Add('Win32_BIOS BIOSVersion :'); ov := Awmi.GetProperty('CodeSet'); if not VarIsNull(ov) then begin if VarIsArray(ov) then begin for i := VarArrayLowBound(ov, 1) to VarArrayHighBound(ov, 1) do begin Memo_Info.Lines.Add(vartostr(VarArrayGet(ov, [i]))); end; end end;
Memo_Info.Lines.Add('-----网卡--------'); Awmi.WMIType := 'Win32_NetworkAdapter'; Memo_Info.Lines.Add('GetEnum:' + Awmi.GetEnum('AdapterTypeID=0')); Memo_Info.Lines.Add('Win32_NetworkAdapter AdapterType :' + vartostr(Awmi.GetProperty('AdapterType', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter name :' + vartostr(Awmi.GetProperty('name', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter MACAddress :' + vartostr(Awmi.GetProperty('MACAddress', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter Caption :' + vartostr(Awmi.GetProperty('Caption', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter DeviceID :' + vartostr(Awmi.GetProperty('DeviceID', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter Description :' + vartostr(Awmi.GetProperty('Description', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter GUID :' + vartostr(Awmi.GetProperty('GUID', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter Index :' + vartostr(Awmi.GetProperty('Index', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter InterfaceIndex :' + vartostr(Awmi.GetProperty('InterfaceIndex', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter Manufacturer :' + vartostr(Awmi.GetProperty('Manufacturer', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter MaxSpeed :' + vartostr(vartostr(Awmi.GetProperty('MaxSpeed', devIndex)))); Memo_Info.Lines.Add('Win32_NetworkAdapter Speed :' + vartostr(Awmi.GetProperty('Speed', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter NetConnectionStatus :' + vartostr(Awmi.GetProperty('NetConnectionStatus', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter PNPDeviceID :' + vartostr(Awmi.GetProperty('PNPDeviceID', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter ProductName :' + vartostr(Awmi.GetProperty('ProductName', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter ServiceName :' + vartostr(Awmi.GetProperty('ServiceName', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter SystemName :' + vartostr(Awmi.GetProperty('SystemName', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter PermanentAddress :' + vartostr(Awmi.GetProperty('PermanentAddress', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapter NetworkAddresses :'); ov := Awmi.GetProperty('NetworkAddresses', devIndex); if not VarIsNull(ov) then begin if VarIsArray(ov) then begin for i := VarArrayLowBound(ov, 1) to VarArrayHighBound(ov, 1) do begin Memo_Info.Lines.Add(vartostr(VarArrayGet(ov, [i]))); end; end end; Memo_Info.Lines.Add('-----网卡设置--------'); Awmi.WMIType := 'Win32_NetworkAdapterConfiguration'; Memo_Info.Lines.Add('GetEnum:' + Awmi.GetEnum('IPEnabled=True')); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration Properties :' + vartostr(Awmi.GetProperty('Properties', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration Caption :' + vartostr(Awmi.GetProperty('Caption', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration Description :' + vartostr(Awmi.GetProperty('Description', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration DNSDomain :' + vartostr(Awmi.GetProperty('DNSDomain', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration DNSHostName :' + vartostr(Awmi.GetProperty('DNSHostName', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration Index :' + vartostr(Awmi.GetProperty('Index', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration InterfaceIndex :' + vartostr(Awmi.GetProperty('InterfaceIndex', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration MACAddress :' + vartostr(Awmi.GetProperty('MACAddress', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration ServiceName :' + vartostr(Awmi.GetProperty('ServiceName', devIndex))); Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration DefaultIPGateway :'); ov := Awmi.GetProperty('DefaultIPGateway', devIndex); if not VarIsNull(ov) then begin if VarIsArray(ov) then begin for i := VarArrayLowBound(ov, 1) to VarArrayHighBound(ov, 1) do begin Memo_Info.Lines.Add(vartostr(VarArrayGet(ov, [i]))); end; end end;
Memo_Info.Lines.Add('Win32_NetworkAdapterConfiguration IPAddress :'); ov := (Awmi.GetProperty('IPAddress', devIndex)); if not VarIsNull(ov) then begin if VarIsArray(ov) then begin for i := VarArrayLowBound(ov, 1) to VarArrayHighBound(ov, 1) do begin Memo_Info.Lines.Add(vartostr(VarArrayGet(ov, [i]))); end; end end;
基于WMI的信息查询和编辑,按微软的说明一般都是 Requirements Minimum supported client Windows Vista Minimum supported server Windows Server 2008 Namespace Root\CIMV2 MOF CIMWin32.mof DLL CIMWin32.dll