不能将类型(Null)的变体转换为类型(OleStr)

时间:2022-03-10 16:23:45

Do you know why the block of code bellow will negate the "Could not convert variant of type (Null) into type (OleStr)" on some computers, not all of them but 3 out of ten computers generate the error message.

您知道为什么bellow的代码块会在某些计算机上否定“不能将类型(Null)类型的变体转换为类型(OleStr)”吗?不是所有的,而是10台计算机中的3个生成错误消息。

不能将类型(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, Format('%s',[String(FWbemObject.Version)]));
      FWbemObject:=Unassigned;
    end;

end;

The function is then executed at FormCreate:

然后在FormCreate中执行该函数:

GETWMIstringSW('Win32_Product','Name');

Thank you so much for your help.

非常感谢你的帮助。

4 个解决方案

#1


12  

You code fails when the value of a WMI property returns null. You can fix this, checking if the property has a null value before to cast or convert to an string. For this task you can use the VarIsNull function or just use the VarToStr method to safely convert variants to strings like so.

当WMI属性的值返回null时,代码失败。您可以修复这个问题,检查该属性是否具有空值,然后再将其转换为字符串。对于这个任务,您可以使用VarIsNull函数,或者使用VarToStr方法安全地将变量转换成字符串。

 LNode := ClientForm.TreeView1.Items.AddChild(Node, 
              Format('%s',[VarToStr(FWbemObject.Name)]));
 LNode2 := ClientForm.TreeView1.Items.AddChild(LNode, 
              Format('%s',[VarToStr(FWbemObject.Version)]));

#2


2  

To avoid the error message do

为了避免错误消息的发生。

NullStrictConvert := false; // avoid NULL OLE conversion error

NullStrictConvert:= false;//避免零值转换错误。

#3


1  

If you want to null variants to be automatically converted to empty strings, 0 integers, or false booleans, set NullStrictConvert (unit System.Variants) to true.

如果您想要将变量自动转换为空字符串、0个整数或false布尔值,请将NullStrictConvert (unit system . variable)设置为true。

#4


1  

Sometimes, the FWbemObject is not NULL but an exception is raise : "Can'nt convert an Array of Variant in OleStr "

有时,FWbemObject不是NULL,而是一个异常:“不能在OleStr中转换一个变量数组”

As exemple : the BiosVersion (is an array) To solve it, try this :

例如:BiosVersion(是一个数组)来解决它,试试这个:

for I := VarArrayLowBound(FWbemObject.BIOSVersion, 1) to VarArrayHighBound(FWbemObject.BIOSVersion, 1) do L.Add( VarToStr(FWbemObject.BIOSVersion[i]) );

我:= VarArrayLowBound(FWbemObject。BIOSVersion,1)VarArrayHighBound(FWbemObject。BIOSVersion,1)L。添加(VarToStr(FWbemObject.BIOSVersion[我]));

Regards

问候

Zerrouki

Zerrouki

#1


12  

You code fails when the value of a WMI property returns null. You can fix this, checking if the property has a null value before to cast or convert to an string. For this task you can use the VarIsNull function or just use the VarToStr method to safely convert variants to strings like so.

当WMI属性的值返回null时,代码失败。您可以修复这个问题,检查该属性是否具有空值,然后再将其转换为字符串。对于这个任务,您可以使用VarIsNull函数,或者使用VarToStr方法安全地将变量转换成字符串。

 LNode := ClientForm.TreeView1.Items.AddChild(Node, 
              Format('%s',[VarToStr(FWbemObject.Name)]));
 LNode2 := ClientForm.TreeView1.Items.AddChild(LNode, 
              Format('%s',[VarToStr(FWbemObject.Version)]));

#2


2  

To avoid the error message do

为了避免错误消息的发生。

NullStrictConvert := false; // avoid NULL OLE conversion error

NullStrictConvert:= false;//避免零值转换错误。

#3


1  

If you want to null variants to be automatically converted to empty strings, 0 integers, or false booleans, set NullStrictConvert (unit System.Variants) to true.

如果您想要将变量自动转换为空字符串、0个整数或false布尔值,请将NullStrictConvert (unit system . variable)设置为true。

#4


1  

Sometimes, the FWbemObject is not NULL but an exception is raise : "Can'nt convert an Array of Variant in OleStr "

有时,FWbemObject不是NULL,而是一个异常:“不能在OleStr中转换一个变量数组”

As exemple : the BiosVersion (is an array) To solve it, try this :

例如:BiosVersion(是一个数组)来解决它,试试这个:

for I := VarArrayLowBound(FWbemObject.BIOSVersion, 1) to VarArrayHighBound(FWbemObject.BIOSVersion, 1) do L.Add( VarToStr(FWbemObject.BIOSVersion[i]) );

我:= VarArrayLowBound(FWbemObject。BIOSVersion,1)VarArrayHighBound(FWbemObject。BIOSVersion,1)L。添加(VarToStr(FWbemObject.BIOSVersion[我]));

Regards

问候

Zerrouki

Zerrouki