In VBScript, the WScript.Shell.Run
method has three arguments, the third of which is a boolean value specifying whether the program should wait for a newly-spawned process to finish before proceeding.
I am having trouble getting this behaviour to work properly with mstsc.exe
(the Remote Desktop Connection program in Windows).
If I save the below file as test.vbs
and execute it with cscript test.vbs
, it works as expected.
Set obj = CreateObject("WScript.Shell")
Call obj.Run("notepad.exe", 1, true)
MsgBox "You just closed notepad."
Call obj.Run("mstsc.exe", 1, true)
MsgBox "Remote desktop just closed."
However, if I try to execute the same code from an HTA file, it does not work properly -- rather, the message box after running mstsc.exe
appears immediately instead of waiting.
<html>
<head>
<script language="VBScript">
Sub RunProgram
Set obj = CreateObject("WScript.Shell")
Call obj.Run("notepad.exe", 1, true)
MsgBox "You just closed notepad."
Call obj.Run("mstsc.exe", 1, true)
MsgBox "Remote desktop is still open!"
End Sub
</script>
</head>
</body>
<body onload=RunProgram>
</html>
Any idea why this happens and how to fix it?
EDIT: I've tested this on Windows 10 and 7.
Use the 64 bit version of mstsc. c:\windows\sysnative\mstsc.exe
Sysnative
allows 32 bit programs to access System32
directory. A 32 bit program trying to access C:\windows\system32
gets redirected to c:\windows\syswow64
.
From my first comment.
I get the same on Windows 10. I suspect it is to do with HTA being set to run as 32 bit but even changing mstsc to the 32 bit version made no difference