你知道为什么下面的代码块会在某些计算机上否定“无法将类型(Null)的变量转换为类型(OleStr)”,而不是全部,但是十分之三的计算机会生成错误消息.
function GetWMIstringSW(const WMIClass,WMIProperty:string): string; const wbemFlagForwardOnly = $00000020; var FWbemObjectSet: OLEVariant; FWbemObject : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; LNode : TTreeNode; LNode2 : TTreeNode; begin Result:=''; FWbemObjectSet:= FWMIService.ExecQuery(Format('Select %s from %s',[WMIProperty,WMIClass]),'WQL',wbemFlagForwardOnly); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; while oEnum.Next(1,FWbemObject,iValue) = 0 do begin if not VarIsNull(FWbemObject.Properties_.Item(WMIProperty).Value) then Result:=FWbemObject.Properties_.Item(WMIProperty).Value; LNode := ClientForm.TreeView1.Items.AddChild(Node,Format('%s',[String(FWbemObject.Name)])); LNode2 := ClientForm.TreeView1.Items.AddChild(LNode,[String(FWbemObject.Version)])); FWbemObject:=Unassigned; end; end;
然后在FormCreate中执行该函数:
GETWMIstringSW('Win32_Product','Name');
非常感谢你的帮助.