注销或超时时,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 保持为我的机器上的默认值时遇到了麻烦)。