如何让 HTA 自行重启?

发布于 2024-09-16 05:23:01 字数 570 浏览 7 评论 0原文

我有一个 HTML 应用程序,用于构建信息亭环境。在每个会话结束时(当用户单击“注销”或超时后),我希望 HTA 自行关闭并重新启动*。我想知道如何使用 VBScript 以编程方式实现这一目标。

该过程应该像这样

  • 用户单击“注销”或者如果 5 分钟内没有输入
  • 删除临时 Internet 文件
  • 关闭 HTA
  • 重新打开 HTA

这是我遇到问题的重新打开部分。

我确实考虑过的一件事是;在关闭应用程序之前,设置一次性计划任务以再次运行 HTA。不过,我不认为这是一个非常优雅的解决方案:(

感谢你们的帮助!

干杯

Iain


。*我希望 HTA 自行重新启动的原因是,在长时间使用后,奇怪的事情开始发生,我认为与mshta.exe 或 IE 引擎中的错误我的 HTA 非常依赖于动态创建的 IFRAME,并且由于某种原因,HTA 在关闭它们后无法正确清理它们。奇怪的例子是;我生成了一个新的 IFRAME,当我询问 DOM 时,一切看起来都像它应该的那样,但浏览器呈现的是之前聚焦的 IFRAME 中的内容。

I have an HTML Application that I am using to build a kiosk environment. At the end of every session (when the user clicks "Logout" or after a timeout) I want the HTA to close itself and restart*. I was wondering how I would go about achieving this programatically with VBScript.

The process should go something like this

  • User clicks "logout" or if there has been no input for 5 minutes
  • Delete temporary internet files
  • Close HTA
  • Reopen HTA

It's the reopening part I'm having trouble with.

One thing I did consider is; before closing the application, set a once-only scheduled task to run the HTA again. I don't think this is a very elegant solution though :(

Thanks for your help guys!

Cheers

Iain


.* The reason I want the HTA to restart itself is that after extended use, strange things start to happen that I think are related to bugs in mshta.exe or the IE engine. My HTA relys quite heavily on dynamically created IFRAMEs and for some reason the HTA doesn't clean them up properly once they're closed. It's kind of hard to explain the bug in detail but an example of the weirdness is; I spawn a new IFRAME and when I interrogate the DOM everything looks like it should, but what the browser renders is whatever was in the previously focussed IFRAME.

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

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

发布评论

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

评论(2

三生路 2024-09-23 05:23:01

注销或超时时,HTA 运行重新启动脚本,然后退出。重新启动脚本等待 MSHTA 进程消失,然后重新启动它并自行退出。

这可以通过使用 WMI 扫描正在运行的进程并查找命令行参数包含 HTA 名称的 MSHTA.EXE 实例来完成。

这是一个演示这一点的小例子。

这是一个准系统 HTA,将其另存为 RestartTest.hta:

<html> <!-- RestartTest.hta -->
<head>
<title>Self Restarting HTA Test</title>

<script language="VBScript">
sub RunApp(sApp)
  CreateObject("WScript.Shell").Run(sApp)
end sub
sub LogOff()
  RunApp "cscript.exe RestartHTA.vbs"
  MsgBox "simulating delayed exit - click to close"
  window.close()
end sub
</script>
</head>

<body >
<input type="button" value="Log off" onClick="LogOff()" />
</body>
</html>

这是重新启动脚本,另存为 RestartHTA.vbs:

' RestartHTA.vbs'
' wait until RestartTest.hta is not running and restart it.'

MSHTA = "mshta.exe"
sHTA = "RestartTest.hta"

say "waiting for process " & MSHTA & " with param " & sHTA & " to end"
secs = 2000

bRunning = vbTrue
do while bRunning
  if ProcessRunning(MSHTA, sHTA) then
    say "still running, wait a few seconds"
    WScript.Sleep secs
  else
    bRunning = vbFalse
  end if
loop

say "HTA not found, proceding to restart"
WScript.Sleep secs

CreateObject("WScript.Shell").Run(sHTA)
WScript.Quit

'---'

function ProcessRunning(sProcess, sParam)
  set oWMI = GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  set cProcs = oWMI.ExecQuery( _
    "select * from Win32_Process where Name = '" & sProcess & "'")

  bFound = vbFalse
  for each oProc in cProcs
    say oProc.Name & ": " & oProc.CommandLine
    if (InStr(oProc.CommandLine, sParam) > 0) then
      say "found"
      bFound = vbTrue
    else
      say "wrong param"
    end if
  next

  ProcessRunning = bFound
end function

sub Say(s)
  WScript.Echo s
end sub

'==='

您可以运行任一脚本来启动循环,然后只需使用 X 按钮关闭 HTA 即可将其中断。请注意,我显式地使用 CScript 运行重新启动脚本。这只是为了演示目的而显示它的控制台输出(并且因为出于某种原因我无法将 CScript 设置为我的计算机上的默认值)。

When logging off or timing out, the HTA runs a restart script, then exits. The restart script waits for the MSHTA process to go away, then restarts it and exits itself.

This can be done by using WMI to scan running processes and look for an instance of MSHTA.EXE who's command line parameter contains the name of the HTA.

Here is a little example that demonstrates this.

This is a barebones HTA, save it as RestartTest.hta:

<html> <!-- RestartTest.hta -->
<head>
<title>Self Restarting HTA Test</title>

<script language="VBScript">
sub RunApp(sApp)
  CreateObject("WScript.Shell").Run(sApp)
end sub
sub LogOff()
  RunApp "cscript.exe RestartHTA.vbs"
  MsgBox "simulating delayed exit - click to close"
  window.close()
end sub
</script>
</head>

<body >
<input type="button" value="Log off" onClick="LogOff()" />
</body>
</html>

This is the restart script, save as RestartHTA.vbs:

' RestartHTA.vbs'
' wait until RestartTest.hta is not running and restart it.'

MSHTA = "mshta.exe"
sHTA = "RestartTest.hta"

say "waiting for process " & MSHTA & " with param " & sHTA & " to end"
secs = 2000

bRunning = vbTrue
do while bRunning
  if ProcessRunning(MSHTA, sHTA) then
    say "still running, wait a few seconds"
    WScript.Sleep secs
  else
    bRunning = vbFalse
  end if
loop

say "HTA not found, proceding to restart"
WScript.Sleep secs

CreateObject("WScript.Shell").Run(sHTA)
WScript.Quit

'---'

function ProcessRunning(sProcess, sParam)
  set oWMI = GetObject( _
    "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  set cProcs = oWMI.ExecQuery( _
    "select * from Win32_Process where Name = '" & sProcess & "'")

  bFound = vbFalse
  for each oProc in cProcs
    say oProc.Name & ": " & oProc.CommandLine
    if (InStr(oProc.CommandLine, sParam) > 0) then
      say "found"
      bFound = vbTrue
    else
      say "wrong param"
    end if
  next

  ProcessRunning = bFound
end function

sub Say(s)
  WScript.Echo s
end sub

'==='

You can run either one to start the cycle, then just close the HTA with it's X button to break it. Note that I run the restart script with CScript explicitly. This is just to show it's console output for demo purposes (and because I have trouble setting and keeping CScript as the default on my machine for some reason).

暮凉 2024-09-23 05:23:01

看一下下面的代码:

<html>
    <title>HTA Restart</title>
    <head>
    <HTA:APPLICATION
        Id="oHTA"
        Border="thick"
        InnerBorder="no"
        Scroll="no"/>
    </head>
    <script language="vbscript">
        Sub Restart()
            CreateObject("WScript.Shell").Run "mshta " & oHTA.commandLine
            window.close
        End Sub
    </script>
    <body style="background-color: buttonface;">
        <input type="button" value="Logout" onclick="Restart()" />
    </body>
</html>

Take a look at the below code:

<html>
    <title>HTA Restart</title>
    <head>
    <HTA:APPLICATION
        Id="oHTA"
        Border="thick"
        InnerBorder="no"
        Scroll="no"/>
    </head>
    <script language="vbscript">
        Sub Restart()
            CreateObject("WScript.Shell").Run "mshta " & oHTA.commandLine
            window.close
        End Sub
    </script>
    <body style="background-color: buttonface;">
        <input type="button" value="Logout" onclick="Restart()" />
    </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文