VB.NET使用WUApiLib获取IUpdate属性时出现空引用错误
排查VB.NET检查Windows更新时的“Object reference not set to an instance of an object”错误
看起来你的代码已经能正确获取待安装更新的数量,但在遍历更新属性时碰到了空引用异常——这通常是因为Windows Update的部分属性并非所有更新都具备,直接调用ToString()就会触发NullReferenceException。我们一步步来排查和解决:
核心问题原因
1. 部分更新属性可能为Nothing
Windows Update的更新项并非所有属性都有有效值,比如Deadline(截止日期),只有被管理员设置了强制安装期限的更新才会填充这个值,普通更新的Deadline会是Nothing。当你直接对Nothing调用ToString()时,必然会抛出空引用错误。同理,少数更新的LastDeploymentChangeTime也可能存在未赋值的情况。
2. 变量复用与空值未检查
你的代码中直接循环调用WUSearchResults.Updates.Item(i)并访问其属性,没有提前捕获可能的空值,一旦某个属性为Nothing就会触发异常。
修复后的代码(附统计逻辑)
下面的代码不仅修复了空引用问题,还实现了你需要的“统计发布超过2周的更新数量”的需求:
Function CheckWinUpdates() As Integer CheckWinUpdates = 0 Dim WUSession As UpdateSession Dim WUSearcher As UpdateSearcher Dim WUSearchResults As ISearchResult Dim outdatedUpdateCount As Integer = 0 ' 统计发布超2周的更新数量 Dim twoWeeksAgo As DateTime = DateTime.Now.AddDays(-14) Try WUSession = New UpdateSession WUSearcher = WUSession.CreateUpdateSearcher() WUSearchResults = WUSearcher.Search("IsInstalled=0 and Type='Software'") CheckWinUpdates = WUSearchResults.Updates.Count If CheckWinUpdates > 0 Then Try Dim currentUpdate As IUpdate For i = 0 To WUSearchResults.Updates.Count - 1 currentUpdate = WUSearchResults.Updates.Item(i) ' 基础属性输出(确保安全访问) EventLog.WriteEntry("Item is type: " & currentUpdate.ToString, EventLogEntryType.Information, 85) EventLog.WriteEntry("Type: " & currentUpdate.Type.ToString, EventLogEntryType.Information, 85) EventLog.WriteEntry("This windows update is required: " & currentUpdate.Title, EventLogEntryType.Information, 85) ' 空值检查后输出截止日期 If currentUpdate.Deadline IsNot Nothing Then EventLog.WriteEntry("Deadline: " & currentUpdate.Deadline.ToString, EventLogEntryType.Information, 85) Else EventLog.WriteEntry("Deadline: No mandatory deadline set", EventLogEntryType.Information, 85) End If ' 空值检查后输出发布时间,并统计超期更新 If currentUpdate.LastDeploymentChangeTime IsNot Nothing Then EventLog.WriteEntry("Released on: " & currentUpdate.LastDeploymentChangeTime.ToString, EventLogEntryType.Information, 85) ' 判断是否超过2周 If currentUpdate.LastDeploymentChangeTime < twoWeeksAgo Then outdatedUpdateCount += 1 End If Else EventLog.WriteEntry("Released on: Release date unavailable", EventLogEntryType.Information, 85) End If Next ' 输出统计结果 EventLog.WriteEntry($"Total updates released over 2 weeks ago: {outdatedUpdateCount}", EventLogEntryType.Information, 87) Catch ex As Exception EventLog.WriteEntry("Error while logging update details:" & vbCrLf & ex.Message, EventLogEntryType.Error, 86) End Try End If Catch ex As Exception CheckWinUpdates = -1 EventLog.WriteEntry("Error searching for Windows updates:" & vbCrLf & ex.Message, EventLogEntryType.Error, 84) Finally ' 确保资源在无论是否异常时都被清理 WUSearchResults = Nothing WUSearcher = Nothing WUSession = Nothing End Try Return CheckWinUpdates End Function
关键修复点说明
- 新增空值检查:对
Deadline和LastDeploymentChangeTime这类可能为空的属性,先判断IsNot Nothing再调用ToString(),避免空引用。 - 提取当前更新项:用
currentUpdate变量存储每次循环的更新项,减少重复调用Item(i)的开销,也让代码更清晰。 - 实现统计逻辑:通过
twoWeeksAgo计算时间阈值,遍历过程中统计发布超过2周的更新数量,并最终写入事件日志。 - 可靠的资源清理:将对象释放代码移到
Finally块,确保无论是否发生异常,资源都会被正确清理。
内容的提问来源于stack exchange,提问作者Iain
相关产品推荐
相关产品推荐

