问题描述
您好,
我为Excel和Word编写了VBA代码,通过WUAPI访问Windows更新历史记录。 此代码适用于Windows 10之前的所有Windows版本。 在W-10中,当我访问IUpdateHistoryEntry对象的ResultCode属性时,
我收到以下错误:
I have written VBA code for Excel and Word that accesses the Windows update history via the WUAPI. This code works fine in all Windows version prior to Windows 10. In W-10, when I access the ResultCode property of the IUpdateHistoryEntry object, I get the following error:
"运行时错误'-2145120257(80240fff)':对象'IUpdateHistoryEntry2'的方法'ResultCode'失败"
"Run-time error '-2145120257 (80240fff)': Method 'ResultCode' of object 'IUpdateHistoryEntry2' failed"
奇怪的是,它尽管出现错误,但确实会返回正确的值(2 =成功)。 这个错误实际上意味着什么吗? 如果是这样,它告诉我什么以及如何解决导致它的问题,而不仅仅是抑制和忽略
呢?
Curiously, it does actually return the correct value (2 = Succeeded) in spite of the error condition. Does that error actually mean anything? If so, what is it telling me and how do I fix whatever is causing it, without just suppressing and ignoring it?
这是我的代码的演示版本。 只需运行Demo_IUpdateHistoryEntry_Bug例程:
Here's a demonstration version of my code. Just run the Demo_IUpdateHistoryEntry_Bug routine:
Option Explicit
'NOTE: For early binding, requires that the "WUAPI X.Y Type Library" reference be set.
Private Type UpdateHistData
ClientApplicationID As String
DateTime_UTC As Date 'UTC = "Coordinated Universal Time" (formerly GMT). To convert to local time,
'see the B_GenUtils module's function DateTimeUtcToLocal or Chip Pearson's
'GMTTimeToLocalTimeSerial function.
Description As String
ResultCode As Integer
SupportURL As String
Title As String
UpdateID As String
UpdateRevisionNumber As Long
End Type
Private Enum OperationResultCode
orcNotStarted = 0
orcInProgress = 1
orcSucceeded = 2
orcSucceededWithErrors = 3
orcFailed = 4
orcAborted = 5
End Enum
Public Sub Demo_IUpdateHistoryEntry_Bug()
'
'Prints the date and title of the latest updates to the Immediate window.
'
'To demonstrate the Windows-10 bug, comment out the "On Error Resume Next" line in the GetUpdateInfo function,
'below (marked with "***") and then run this routine under Windows 10.
'
'*************************************************************************************************************
Dim i As Long
Dim LatestUpdateDT As Date
Dim UpdateList() As UpdateHistData
UpdateList = GetUpdateInfo()
'Deterimine the latest update date
For i = LBound(UpdateList) To UBound(UpdateList)
If UpdateList(i).ResultCode >= orcSucceeded Then
'Is a currently installed update, so check its date
If UpdateList(i).DateTime_UTC > LatestUpdateDT Then LatestUpdateDT = UpdateList(i).DateTime_UTC
End If
Next i
'Print all updates that are current
For i = LBound(UpdateList) To UBound(UpdateList)
If UpdateList(i).DateTime_UTC >= LatestUpdateDT Then
Debug.Print UpdateList(i).DateTime_UTC & ": " & UpdateList(i).Title & _
", Result = " & UpdateList(i).ResultCode
End If
Next i
End Sub
Private Function GetUpdateInfo() As UpdateHistData()
'
'Returns an array of update-history elements that are likely to be useful, as defined by the UpdateHistData
'data-type (defined above), for all update-history records.
'
'*************************************************************************************************************
' 'Late binding:
' Dim EntryObj As Object
' Dim HistoryColl As Variant
' Dim SessionObj As Object
' Dim SearcherObj As Object
'Early binding requires that the "WUAPI X.Y Type Library" reference be set:
Dim EntryObj As IUpdateHistoryEntry
Dim HistoryColl As IUpdateHistoryEntryCollection
Dim SessionObj As IUpdateSession
Dim SearcherObj As IUpdateSearcher
Dim HistoryCount As Integer
Dim i As Long
Set SessionObj = CreateObject("Microsoft.Update.Session")
Set SearcherObj = SessionObj.CreateUpdateSearcher
HistoryCount = SearcherObj.GetTotalHistoryCount
Set HistoryColl = SearcherObj.QueryHistory(1, HistoryCount)
ReDim TempArray(1 To HistoryColl.Count) As UpdateHistData
For Each EntryObj In HistoryColl
i = i + 1
TempArray(i).ClientApplicationID = EntryObj.ClientApplicationID
TempArray(i).DateTime_UTC = EntryObj.Date 'UTC = "Coordinated Universal Time" (formerly GMT). To
'convert to local time, see the B_GenUtils module's function
'DateTimeUtcToLocal or Chip Pearson's GMTTimeToLocalTimeSerial
'function.
TempArray(i).Description = EntryObj.Description
'WINDOWS-10 BUG: Windows 10 has an apparent bug in which the IUpdateHistoryEntry object can return a valid
'ResultCode value of "Succeeded" (2) but it also throws the error
'
' "Run-time error '-2145120257 (80240fff)': Method 'ResultCode' of object 'IUpdateHistoryEntry2' failed"
'
'for no apparent reason. Furthermore, note that it doesn't return result code "SucceededWithErrors" (3) in
'such cases. So ignore all such errors:
' On Error Resume Next '*** To demonstrate the bug, comment-out this line and run under Windows 10
TempArray(i).ResultCode = EntryObj.ResultCode
On Error GoTo 0
TempArray(i).SupportURL = EntryObj.SupportURL
TempArray(i).Title = EntryObj.Title
TempArray(i).UpdateID = EntryObj.UpdateIdentity.UpdateID
TempArray(i).UpdateRevisionNumber = EntryObj.UpdateIdentity.RevisionNumber
Next
GetUpdateInfo = TempArray()
End Function
预先感谢您的帮助
推荐答案
这篇关于Windows Update API方法IUpdateHistoryEntry.ResultCode在Windows 10中引发错误“-2145120257”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!