Search code examples
vbscripthtatasklist

Way to close child HTA file if it gets called again from a parent HTA file


I want to run a HTA file where, there is a loop in which the parent HTA calls a child HTA to display an update on a regular interval. I want the child HTA to be remain open with old update and it should close when it is called again with new update and diplay it. I tried to do it, but I am unable to add close HTA condition on the child HTA. Which results in all Child HTA opened in the background.

Parent HTA File,

Code given below

<html>
<head>
<title>Parent Application</title>
<HTA:APPLICATION
  APPLICATIONNAME="Parent Application"
  ID="ParentApplication"
  VERSION="1.0"/>
</head>

<script language="VBScript">

Sub OnClickButtonConnect()
  Dim currentDirectory,pos
  pos=InStrRev(document.location.pathname,"\")
  currentDirectory=""
  If pos>0 Then
    currentDirectory = Left(document.location.pathname,pos)
  End If

  Dim WshShell, i, g
  g = 5
  set WshShell = CreateObject("wscript.Shell")
  For i = 1 To g
  cmdline = "mshta.exe """ & currentDirectory & "child.hta"" """ & login.value & """ """ & password.Value & """"
  WshShell.Run cmdline,1,False
  next
  window.close
End Sub
</script>

<body bgcolor="white">

<!--Add your controls here-->

Login:<input type="text" name="login" id="login"><BR>
Password:<input type="password" name="password" id="password"><BR>
<input type="button" name="Connect" id="Connect" value="Connect" onclick="OnClickButtonConnect">
<!--{{InsertControlsHere}}-Do not remove this line-->
</body>
</html>

Child HTA

<html>
<head>
<title>Child Application</title>
<HTA:APPLICATION
  APPLICATIONNAME="Child Application"
  ID="ChildApplication"
  VERSION="1.0"/>
</head>

<script language="VBScript">

Sub Window_OnLoad
  str=""
  arguments = Split(ChildApplication.CommandLine," """)
  For i=0 To UBound(arguments)
    arguments(i)=Replace(arguments(i),"""","")
  Next
  document.body.innerhtml="login is: " & arguments(1) & "<BR>password is: " &  arguments(2)
End Sub

</script>

<body bgcolor="white">

<!--Add your controls here-->

<!--{{InsertControlsHere}}-Do not remove this line-->
</body>
</html>

Solution

  • Call this Sub before opening the child hta. Make sure the name of the hta matches its actual name.

    Sub CloseChild
        Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
        Set colProcessList = objWMIService.ExecQuery _
            ("Select CommandLine from Win32_Process where CommandLine like '%child.hta%'")
        For Each objProcess In colProcessList
            objProcess.Terminate()
        Next 
    End Sub
    

    Edit: I just wanted to comment for anyone who may read this later. Putting CommandLine in the select statement is not explicitly required, even though this property is used in the where clause. You can select any or all of the properties in the Win32_Process class, including or excluding CommandLine.

    I do suggest selecting only the properties you need to increase the speed of the query, and historically, for clarity, I select the same property as I use in the where clause if I don't actually need one.