开发者

Using callback functions in vbscript

开发者 https://www.devze.com 2023-04-12 09:30 出处:网络
I\'m trying to make an u开发者_如何学运维pdate script for windows7 in vbscript when invoking IUpdateSearcher::BeginSearch how do I pass the callback to ISearchCompletedCallback::Invoke Method?

I'm trying to make an u开发者_如何学运维pdate script for windows7 in vbscript

when invoking IUpdateSearcher::BeginSearch how do I pass the callback to ISearchCompletedCallback::Invoke Method?

I'm just clueless on this points:

  • do I need an function or sub or does this a custom object with an invoke method(and how to create)
  • how I need to pass the callback
  • is it even possible in vbscript (if not what is a good next step?)

Thanks


VBscript does not deal well with callbacks.

I've been working with the BeginSearch, BeginDownload and BeginInstall for a while, and the solution is to use any object descendant of iUnknown and ignore it for the rest of the code.

You will have to use the IsCompleted property of the returned objects iSearchJob, iDownloadJob or iInstallationJob, to verify that the operation has been completed, before calling EndSearch, EndDownload or EndInstall to finish the operation and retrieve valid results.

You should also implement a timeout function to terminate each operation if it has been running for too long.

Here is a simple example that works (run it with CScript):

Dim strChars
    strChars = "-+|+"
If InStr(LCase(WScript.FullName),"cscript.exe") = 0 Then 
    WScript.Echo "This script requires to be run using CScript.exe"    
    WScript.Quit
End If
Set objUpdateSearcher = CreateObject("Microsoft.Update.Session").CreateUpdateSearcher()
WScript.Echo "Searching for new updates ..."
Set objSearchJob = objUpdateSearcher.BeginSearch ("", WScript , vbNull)
Do Until objSearchJob.IsCompleted
    WScript.Sleep 200
    WScript.StdOut.Write GetProgress & vbCr
Loop
Set objSearchResult = objUpdateSearcher.EndSearch (objSearchJob)
If objSearchResult.ResultCode = 2 Then
    If objSearchResult.Updates.Count = 0 Then
        WScript.Echo "No new updates."
    ElseIf objSearchResult.Updates.Count = 1 Then
        WScript.Echo "A new update is available."
    Else 
        WScript.Echo objSearchResult.Updates.Count & " new updates available."
    End If
Else
    WScript.Echo "Error while searching updates (Code: 0x" & Hex(objSearchResult.ResultCode) & ")"
End If
Function GetProgress
    GetProgress = Left(strChars,1)
    strChars = Right(strChars,Len(strChars)-1) & Left(strChars,1)
End Function


I've never tried it, but I'd look at the ConnectObject Method. This article about scripting events might also be useful.

So maybe something like this (complete guess):

Set objSession = CreateObject("Microsoft.Update.Session")
Set objSearcher = objSession.CreateUpdateSearcher
WScript.ConnectObject objSearcher, "searcherCallBack_"
objSearcher.BeginSearch ...


sub searcherCallBack_Invoke()
    ' handle the callback
end sub

I'd also suggest reading Guidelines for Asynchronous WUA Operations to make sure that you clean up after yourself.
that link also mentions using Windows Script Host, so it should definitely be possible to do this, though unless you need it to be asynchronous, the synchronous methods wouls probably be a easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号