我编写了一个简单的函数来使用 WMI 检索系统信息,将类和属性名称作为参数传递。当我执行这样的功能时
Writeln('Procesor Id '+GetWMIInfo('Win32_Processor','Name'));
Writeln('Mother Board Serial '+GetWMIInfo('Win32_BaseBoard','SerialNumber'));
Writeln('BIOS Version '+GetWMIInfo('Win32_BIOS','Version'));
执行时间约为 1300 ms。
我需要检索很多附加信息,那么是否可以减少此函数的执行时间?
这是一个带有函数的示例应用程序
{$APPTYPE CONSOLE}
uses
Diagnostics,
SysUtils,
ActiveX,
ComObj,
Variants;
function GetWMIInfo(const WMIClass, WMIProperty:string): string;
var
sWbemLocator : OLEVariant;
sWMIService : OLEVariant;
sWbemObjectSet: OLEVariant;
sWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
Result:='';
sWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
sWMIService := sWbemLocator.ConnectServer('', 'root\CIMV2', '', '');
sWbemObjectSet:= sWMIService.ExecQuery('SELECT * FROM '+WMIClass,'WQL');
oEnum := IUnknown(sWbemObjectSet._NewEnum) as IEnumVariant;
if oEnum.Next(1, sWbemObject, iValue) = 0 then
Result:=sWbemObject.Properties_.Item(WMIProperty).Value;
end;
var
SW : TStopwatch;
begin
try
CoInitialize(nil);
try
SW.Reset;
SW.Start;
Writeln('Procesor Id '+GetWMIInfo('Win32_Processor','Name'));
Writeln('Mother Board Serial '+GetWMIInfo('Win32_BaseBoard','SerialNumber'));
Writeln('BIOS Version '+GetWMIInfo('Win32_BIOS','Version'));
SW.Stop;
Writeln('Elapsed ms '+FormatFloat('#,0.000',SW.Elapsed.TotalMilliseconds));
finally
CoUninitialize;
end;
except
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Readln;
end.