【问题标题】:Accessing IUpdate2, 3, 4, or 5 from ISearchResult (Windows Update Search)从 ISearchResult 访问 IUpdate2、3、4 或 5(Windows 更新搜索)
【发布时间】:2014-11-24 07:20:12
【问题描述】:

我正在尝试检索 Windows Update 搜索结果,其类型为 IUpdate

但是,我想要的属性不是 IUpdate 接口的一部分。我想要的属性(BrowseOnly)位于IUpdate3 接口内。

我的问题是,如何从包含IUpdate 的搜索结果中访问 IUpdate3 接口中的属性?或者我可以改变搜索返回的界面吗?

【问题讨论】:

  • 我从 MSDN 文档中看到 IUpdate3 继承自 IUpdate(通过 IUpdate2)。您是否尝试投射到IUpdate3?显示您的搜索代码可能会有所帮助。

标签: vb.net interface windows-update


【解决方案1】:

事实证明,我的代码中没有对 WUApi.DLL 的引用。输入该引用后,我成功地将结果转换为 IUpdate3,从而使我能够访问 BrowseOnly 字段。

见下文(IUpdate3 在代码块中间使用)...

''' <summary>
''' Performs a windows update check for any updates that are...
''' A: Not installed
''' B: Not hidden
''' C: Software updates (OS and software, no hardware updates)
''' </summary>
''' <returns>0 on success, 1 on failure</returns>
''' <remarks></remarks>
Function checkForUpdates() As Integer

    Dim updateSession  ' Object to hold our MS Update Session
    Dim updateSearcher ' Object to perform our MS Win Update Search
    Dim results        ' Object to hold our MS Win Update Search Results
    Dim stopWatch As New Stopwatch()
    stopWatch.Start()

    outputWriter.WriteLine("----WINDOWS UPDATES-----@ " & Now, False)
    outputWriter.WriteLine("  -We are beginning our update search. Please note, this may take a few minutes." & _
                           "   On Windows Server 2003 this may take 800 years.", False)

    ' We cannot guarantee the update check will succeed, so use a try catch in case of error.
    Try
        updateSession = CreateObject("Microsoft.Update.Session")
        updateSearcher = updateSession.CreateUpdateSearcher()
        results = updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
    Catch ex As Exception
        outputWriter.WriteLine("  ERROR: Something went wrong in our update search. Details below...", False)
        outputWriter.WriteLine("  Error Msg: " & ex.Message, False)
        Return 1
    End Try

    outputWriter.WriteLine("  -Windows update search has successfully completed. Beginning iteration of result set...", False)

    ' Similar to above, we cannot guarantee iterating through our result set will succeed. Use a try catch.
    Try
        Dim totalUpdates = results.Updates.Count
        outputWriter.WriteLine("-----Windows Updates-----@ " & Now, True)
        If results.Updates.Count = 0 Then
            outputWriter.WriteLine("Total Updates: 0", True)
            outputWriter.WriteLine("Important:     0", True)
            outputWriter.WriteLine("Optional:      0", True)
        Else
            Dim imp_updates = 0
            Dim opt_updates = 0
            For i = 0 To totalUpdates - 1
                Dim update = results.Updates.Item(i)
                If CType(update, WUApiLib.IUpdate3).BrowseOnly = True Then ' BrowseOnly is specifically used for whether an update is deemed optional or not (True for optional)
                    opt_updates = opt_updates + 1
                Else
                    imp_updates = imp_updates + 1
                End If
            Next

            outputWriter.WriteLine("Total Updates: " & totalUpdates, True)
            outputWriter.WriteLine("Important:     " & imp_updates, True)
            outputWriter.WriteLine("Optional :     " & opt_updates, True)

        End If
        stopWatch.Stop()
        If stopWatch.ElapsedMilliseconds >= 1000 Then
            outputWriter.WriteLine("--------Total Time: " & Math.Round((CDbl(stopWatch.ElapsedMilliseconds) / 1000), 2) & " Sec----------------" & vbCrLf, True)
        Else
            outputWriter.WriteLine("--------Total Time: " & stopWatch.ElapsedMilliseconds & " MS-------------------" & vbCrLf, True)
        End If

    Catch ex As Exception
        outputWriter.WriteLine("  ERROR: We encountered an issue while iterating through Windows Update Search Results. Details below...", False)
        outputWriter.WriteLine("  Error Msg: " & ex.Message, False)
        Return 1
    End Try

    outputWriter.WriteLine("  -Iteration of Windows Update Search Results has successfully completed.", False)
    outputWriter.WriteLine("-------------------------" & vbCrLf, False)

    ' Clean up our objects.
    updateSession = Nothing
    updateSearcher = Nothing
    results = Nothing

    ' Success!
    Return 0
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-20
    • 2010-09-19
    • 1970-01-01
    • 2017-03-28
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多