无法将类型(Null)的变体转换为类型(OleStr)

编程入门 行业动态 更新时间:2024-10-26 00:19:47
本文介绍了无法将类型(Null)的变体转换为类型(OleStr)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您知道为什么下面的代码块会否定某些计算机上的无法将类型(Null)的变体转换为类型(OleStr)",不是所有的计算机,而是十分之三的计算机都会生成错误消息.

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.

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;

然后在FormCreate上执行该函数:

The function is then executed at FormCreate:

GETWMIstringSW('Win32_Product','Name');

非常感谢您的帮助.

推荐答案

当WMI属性的值返回null时,您的代码将失败.您可以解决此问题,在转换或转换为字符串之前检查属性是否具有null值.对于此任务,您可以使用 VarIsNull 函数或仅使用 VarToStr 方法可将变体安全地转换为类似的字符串.

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.

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

更多推荐

无法将类型(Null)的变体转换为类型(OleStr)

本文发布于:2023-10-14 21:43:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   变体   转换为   OleStr   Null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!