2

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Can't reproduce. The second message box only appears after I close the RDP connection in both cases. – Ansgar Wiechers Jul 22 '16 at 23:39
  • @AnsgarWiechers What operating system are you using? I should have mentioned I'm experiencing this on Windows 10... (have tried it on a couple PCs though). – Stephen Humphries Jul 23 '16 at 00:03
  • 1
    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. –  Jul 23 '16 at 00:26
  • My test box was a 32-bit Windows 7. I can confirm the behavior you describe (and the solution Noodles proposed) on a 64-bit system (Windows Server 2012 R2 ). – Ansgar Wiechers Jul 23 '16 at 11:36

1 Answers1

4

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

  • 2
    for more information [File System Redirector](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx) – Kul-Tigin Jul 23 '16 at 00:50
  • 1
    Huh, that's interesting. Thanks for your help! This fixed it for me. – Stephen Humphries Jul 23 '16 at 00:58
  • Another option is to run the HTA with the 64-bit interpreter (`C:\Windows\System32\mshta.exe`). For some reason Microsoft decided to make the 32-bit `mshta.exe` the default interpreter even on 64-bit versions of the OS. VBScript files are run with the 64-bit version of `cscript.exe` by default. If you run them with the 32-bit version (from `C:\Windows\SysWOW64`) you get the same behavior as with the HTA. – Ansgar Wiechers Jul 23 '16 at 11:44