需要uses ActiveX, ComObj
调用方法:
CoInitialize(nil); // 初始化 COM
try
GetHardwareRecords(mmo1);
// 将结果输出到 Memo 控件
finally
CoUninitialize; // 反初始化 COM
end;
procedure GetHardwareRecords(memo: TMemo);
var
wmi: OleVariant;
deviceList: TStringList;
devices: OleVariant;
enumDevices: IEnumVariant;
fetchedDevices: LongWord;
i: Integer;
info: string;
function GetHardwareSize(device: OleVariant): string;
var
size: Double;
begin
Result := '';
size := 0;
if VarIsEmpty(device) then
Result := ''
else begin
try
try
if not VarIsNull(device.Properties_.Item('Capacity').Value) then
size := device.Properties_.Item('Capacity').Value / 1024 / 1024 / 1024 // 转换为GB
except
size := 0;
end;
try
if (size = 0) and (not VarIsNull(device.Properties_.Item('Size').Value)) then
size := device.Properties_.Item('Size').Value / 1024 / 1024 / 1024; // 转换为GB
except
size := 0;
end;
if size<>0 then
Result := Format('%.2f GB', [size]);
except
Result := '';
end;
end;
end;
begin
wmi := CreateOleObject('WbemScripting.SWbemLocator');
wmi := wmi.ConnectServer('.', 'root\cimv2', '', '');
deviceList := TStringList.Create;
try
deviceList.Add('CPU 处理器=Win32_Processor');
deviceList.Add('主板=Win32_BaseBoard');
deviceList.Add('BIOS=Win32_BIOS');
deviceList.Add('硬盘=Win32_DiskDrive');
deviceList.Add('网卡=Win32_NetworkAdapter');
deviceList.Add('内存=Win32_PhysicalMemory');
deviceList.Add('电池=Win32_Battery');
deviceList.Add('风扇=Win32_Fan');
deviceList.Add('IDE=Win32_IDEController');
for i := 0 to deviceList.Count - 1 do
begin
devices := wmi.ExecQuery('SELECT * FROM ' + deviceList.ValueFromIndex[i]);
enumDevices := IUnknown(devices._NewEnum) as IEnumVariant;
fetchedDevices := 0;
while enumDevices.Next(1, devices, fetchedDevices) = 0 do
begin
info:='';
if VarToStr(devices.Caption) = VarToStr(devices.Name) then
info := VarToStr(devices.Caption)
else
info := VarToStr(devices.Caption) + ' ' + VarToStr(devices.Name);
memo.Lines.Add('kind=' + deviceList.Names[i]);
memo.Lines.Add('info=' + info + ' ' + GetHardwareSize(devices));
//memo.Lines.Add('info=' + VarToStr(devices.Caption) + ' ' + VarToStr(devices.Name) + ' ' + GetHardwareSize(devices));
end;
end;
finally
deviceList.Free;
end;
end;