在vbscript中使用回调函数

发布于 2024-12-09 09:39:33 字数 474 浏览 0 评论 0 原文

我试图

在调用 IUpdateSearcher::BeginSearch 如何将回调传递给 ISearchCompletedCallback::调用方法

我对这一点一无所知:

  • 我是否需要一个函数或子函数,或者这是一个带有调用方法的自定义对象(以及如何创建),
  • 我需要如何传递回调
  • ,这在vbscript中是否可能(如果不是的话是什么)下一步好吗?)

谢谢

I'm trying to make an update 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2024-12-16 09:39:33

VBscript 不能很好地处理回调。

我一直在使用 BeginSearch ,<一href="https://learn.microsoft.com/en-us/windows/win32/api/wuapi/nf-wuapi-iupdatedownloader-begindownload" rel="nofollow noreferrer">开始下载BeginInstall 有一段时间,解决方案是使用 iUnknown 的任何对象后代,并在其余代码中忽略它。

您必须使用返回对象的 IsCompleted 属性 iSearchJob, iDownloadJobiInstallationJob,在调用EndSearchEndDownloadEndInstall完成操作之前验证操作是否已完成并检索有效结果。

您还应该实现一个超时功能,以便在每个操作运行时间过长时终止该操作。

这是一个有效的简单示例(使用 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

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
清眉祭 2024-12-16 09:39:33

我从未尝试过,但我会查看 ConnectObject 方法。
这篇关于脚本事件的文章可能也有用。

所以也许是这样的(完全猜测):

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


sub searcherCallBack_Invoke()
    ' handle the callback
end sub

我还建议阅读 异步 WUA 操作指南,以确保您自行清理。
该链接还提到使用 Windows Script Host,因此绝对可以做到这一点,尽管除非您需要异步,否则同步方法可能会更容易。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文