start /wait works in explorer but not from within TC ?!?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
larry99
Junior Member
Junior Member
Posts: 44
Joined: 2009-01-16, 16:03 UTC
Location: Germany

start /wait works in explorer but not from within TC ?!?

Post by *larry99 »

I am writing a batch file where I want to start remote desktop and do some preparation/cleanup before and after. It all boils down to a requirement to have this command work as expected, i.e. really wait:

start /wait mstsc.exe

This is what I did:
1. In TC command line enter "cmd"<enter> then insert above command
=> returns to command prompt without wait

2. In Window Start Menu Enter "cmd" in input box then <enter> then insert above command
=> waits as expected until Remote Desktop is closed

3. Enter above command in a batch file with a second command (just "pause") on the next line. Copy the batch file to desktop

4. Start the copy by double click on the desktop icon
=> "pause" prompt only appears after I close Remote Desktop

5. Navigate to the same batch file within TC and double click it there
=> prompt appears at once without waiting

What is so different within TC that the "/wait" parameter has no effect whereas outside of TC it just works as expected?
And even more important: how can I fix it? I would like to be able to run the batch file from within Total Commander.
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

What I see, if I execute mentioned command line from TC, or if I run start /wait mscsc.exe from cmd.exe, cmd.exe doesn't wait for mstsc termination and returns immediately (if it is started from TC, it just exits, if it is started separately, I can run next commands without closing mstsc.exe)... So it works in the same way from TC or from cmd.exe.

However it does matter which cmd.exe I use - 32-bit or 64-bit one, and it seems that you use 32-bit TC under 64-bit Windows so in such case 32-bit cmd.exe is started from TC by default and 64-bit one is started from Explorer. And 32-bit one really doesn't wait for mstsc.exe termination.

Just try these commands from TC:

Code: Select all

C:\Windows\System32\cmd.exe /c "start /wait mstsc.exe & set PROCESSOR_ARCHITECTURE & pause"
C:\Windows\Sysnative\cmd.exe /c "start /wait mstsc.exe & set PROCESSOR_ARCHITECTURE & pause"
(second one bypasses WOW64 redirection and starts 64-bit cmd.exe from TC and it does wait for mstsc termination)

Alternatively, you can try both from Explorer, but with small modification:

Code: Select all

C:\Windows\SysWOW64\cmd.exe /c "start /wait mstsc.exe & set PROCESSOR_ARCHITECTURE & pause"
C:\Windows\System32\cmd.exe /c "start /wait mstsc.exe & set PROCESSOR_ARCHITECTURE & pause"
(no redirection is applied to 64-bit process so second one starts 64-bit cmd.exe from Explorer directly and it does wait for mstsc termination)
Last edited by MVV on 2017-05-25, 14:41 UTC, edited 1 time in total.
User avatar
Dalai
Power Member
Power Member
Posts: 9387
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

Do you use TC 32 bit on Windows 64 bit? Then use this batch:

Code: Select all

@echo off

setlocal
set mstsc=%SystemRoot%\system32\mstsc.exe
if exist %SystemRoot%\Sysnative\mstsc.exe set mstsc=%SystemRoot%\Sysnative\mstsc.exe

start /wait "" "%mstsc%"
endlocal
Why so complicated? Well, to understand this, one needs to know that there's two different mstsc.exe on a Windows 64 bit system, one in \Windows\system32 and one in \Windows\SysWOW64. Running mstsc from a 32 bit program launches the one in \SysWOW64 which then launches the one in \System32 and immediately stops itself. This is why the /wait doesn't seem to have any effect (but it does).

So, the solution is to always use \Windows\system32\mstsc.exe, regardless of the environment. Because of the file-system redirection stuff there's only one way to do that from a 32 bit CMD (or program): use \Windows\Sysnative (a virtual path that's only accessible by 32 bit programs) instead of \Windows\system32.

The above batch works on any Windows system, regardless of architecture and where it's launched from (32 bit or 64 bit CMD).

Regards
Dalai
Last edited by Dalai on 2017-05-25, 16:06 UTC, edited 1 time in total.
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
MVV
Power Member
Power Member
Posts: 8702
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

There is a way of making things much easier and I like it:

Code: Select all

mklink /J %SystemRoot%\System64 %SystemRoot%\System32
This command creates C:\Windows\System64 dir (pointing to C:\Windows\System32 dir) that bypasses redirection so has always the same 64-bit contents in both 32- and 64-bit programs. So after executing it once, you can just use in your batches:

Code: Select all

start /wait %SystemRoot%\System64\mstsc.exe
larry99
Junior Member
Junior Member
Posts: 44
Joined: 2009-01-16, 16:03 UTC
Location: Germany

Post by *larry99 »

Thanks a lot for the explanation and possible fixes. Now it is working just fine!
Post Reply