The life story of the SwitchToThisWindow function

Commenters Mick and Nick (you guys ever considered teaming up and forming a morning radio show?) are interested in the life story of the Switch­To­This­Window function.

The Switch­To­This­Window was originally added in enhanced mode Windows 3.0 in order to support switching out of fullscreen MS-DOS sessions. Recall that enhanced mode Windows 3.0 was actually three operating systems in one: There was a 32-bit virtual machine manager, and inside one virtual machine ran a copy of standard-mode Windows,¹ and inside all the others ran a copy of MS-DOS. This mean that when you pressed a key when in an MS-DOS session, the keyboard interrupt went to the MS-DOS program and not to Windows.

When you pressed Alt+Tab, some crazy magic had to happen. The virtual machine manager had to "un-press" the Alt key in the MS-DOS program, then synchronize the shift states of the Windows virtual machine to match the one from the MS-DOS virtual machine. (For example, if you had the shift key down in the MS-DOS virtual machine, it had to simulate pressing the shift key in the Windows virtual machine so they two shift states were back in sync.) And then it could simulate pressing the Tab key, at which point the Windows virtual machine would see the Alt+Tab sequence and put up the Alt+Tab interface.

That's how things worked if you were running in a windowed MS-DOS session. But if you were in a fullscreen MS-DOS session, things worked differently. Switching back to Windows would mean a display mode reset (which can take a second or longer), and then all the applications on your desktop had to redraw themselves (and probably paging quite a bit in order to do so). This definitely failed to meet the responsiveness people expected from Alt+Tab, so the virtual machine manager pulled a trick: If you pressed Alt+Tab while in a fullscreen MS-DOS session, then instead of switching back to the Windows virtual machine, the virtual machine manager displayed a text-mode version of the Alt+Tab interface.

I will stop to let the craziness of that sink in: The virtual machine manager had its own Alt+Tab interface built out of text mode.

Anyway, when you finally released the Alt key and completed the Alt+Tab sequence, the virtual machine manager needed to tell Windows, "Hey, like, pretend that an Alt+Tab thingie just happened, okay?"

That is what the Switch­To­This­Window function was for. It was the function the virtual machine manager called to tell Windows to switch to a window as if the user had selected it via Alt+Tab (because that is, in fact, what the user did, just via the text-mode interface rather than the graphical one).

A similar thing happened if you pressed Alt+Esc (or Alt+Shift+Esc in a fullscreen MS-DOS session. That's why there's a second parameter to indicate whether the switch should be done "in the style of Alt+Tab" or "in the style of Alt+Esc."

The function was undocumented because it existed only for the virtual machine manager to call in order to coordinate its actions with Windows user interface so that you had one big happy Alt+Tab family.

The text-mode Alt+Tab interface disappeared in Windows 95, but the Switch­To­This­Window function hung around because it wasn't causing anybody any harm, and there was at the time no formal process in place to deprecate and eventually remove an API, not even an internal undocumented one.

In the Windows XP SP1 timeframe, a bunch of lawyers decided that some functions in Windows needed to be documented. The precise rules for determining which functions needed to be documented and which didn't need to be documented were rather complicated. (Some people applied an algorithm different from the ones those lawyers used and came up with a list of functions that are "missing", when all that they really came up with is a list of functions different from the list those lawyers came up with.)²

Anyway, the Switch­To­This­Window function got caught in the dragnet, so it got documented. Mind you, like it says right at the top of the documentation, there is no guarantee that the function will continue to exist; it can vanish at any time. Although there is documentation, it has the logical status of an internal function, and internal functions have a tendency to change or vanish entirely. Perhaps someday a new chapter will be added to the life story of Switch­To­This­Window: "The Switch­To­This­Window was removed in Windows Q" for some value of Q.


¹ Not true, but true enough. Don't make me bring back the Nitpicker's Corner.

² I will delete any comments on the subject of the algorithm by which those lawyers determined which functions needed to be documented, or on the documentation itself.

Bonus chatter: As far as I can determine, Switch­To­This­Window just does a Set­Foreground­Window on the window you're switching to, possibly posting it a WM_SYS­COMMAND/SC_RESTORE message, and moving the previous foreground window to the bottom of the Z-order if switched via Alt+Esc. It doesn't provide any special secret sauce for bypassing the normal foreground activation rules. The process that calls Switch­To­This­Window still requires foreground-change permission.

Comments (36)
  1. MikeCaron says:

    That's really interesting. I don't remember the text-version of Alt-Tab back when I used Windows 3.1. Then again, I probably didn't know about Alt-Tab way back then either!

  2. NB says:

    Me neither. Too bad my old Windows 3.1 machine is no longer around to test this :(

    Might be virtual machine manager mentioned in the post still be around in some form?

  3. Crescens2k says:

    This kind of post is always enjoyable. This also highlighted again how interesting Windows 3.x was internally.

  4. I remember the text mode switcher… As I recall, the old MS-DOS Shell program seemed to use the same switcher.  At least, it looked the same…  Seemed fast enough, although I thought it odd that there was a difference.

    I love these posts that reflect on decisions that date back to the pre-Win32 days.  It's interesting stuff!

  5. (absent-mindedly clicked Post before I was done…)

    "although I thought it odd that there was a difference" between the graphical task switcher and text mode task switcher – i.e. why the text mode switcher had to exist

  6. Ben Cooke says:

    Back when I was on Windows 3 I was using a 286 so I was in Standard Mode. I remember atext-mode switcher here too but I assume the implementation was pretty different despite the similar UI…

  7. Grijan says:

    I, too, remember the text mode switcher fondly. Back then, I ran many MS-DOS applications, and sometimes I used Windows solely as a multitasking engine to let me quickly switch between DOS programs (ah! the good old days of text-mode editors and command line compilers!). Then I switched to NT 4. It went off smoothly (I had 16 MB of RAM), but if/when something crashed, the system itself kept up.

  8. xpclient says:

    There are now three Alt-Tabs in Windows now. One you get with Aero whose previews are hard to make out due to small size of thumbnails, then the ugly gray one which you get if you disable the Aero one and the beautiful one but with classic Alt-Tab style present only in Vista/Windows 7 Home Basic/Starter. Can you forward my request to replace the ugly gray Alt-Tab with the nicer blue one? (See screenshot:…/niceralttab.png)

  9. voo says:

    Wow after all those years I just stumbled upon a new shortcut (Alt+Esc) – not especially useful but still :D

  10. alegr1 says:

    Speaking of text-mode "GUI", pressing F7 in CMD window gets you a nice command history list.

  11. Joshua says:

    @xpclient: Wow that's an odd bit of lacking code reuse.

  12. RichardDeeming says:

    @xpclient: The small alt-tab thumbnails are less of an issue in 7 – just pause on the thumbnail, and you'll see the full window, just like the Aero Peek feature from the taskbar.

  13. Anonymous Coward says:

    I think that one of the lesson to be learnt from this, is that you have to be careful when designing a system to make it impossible for ‘normal’ applications to call internal functions, even if that function cannot do any harm.

  14. Brian says:

    @xpclient: I don't understand what you're asking.  If you select the "Windows 7 Basic" theme you get the blue alt+tab, and if you select the "Windows Classic" theme you get the old gray one.

  15. xpclient says:

    @Brian, there is a registry value using which you can keep Aero on and yet use Classic Alt Tab if it suits you better.

    Windows Registry Editor Version 5.00



    I find the Classic Alt Tab more usable than the Aero Alt-Tab but ugly especially when the blue Alt-Tab already exists.

  16. Blue alt tab says:

    The "blue" alt tab is themed (Vista Basic). If you use classic mode, that means no theme, and no "blue" alt tab.

  17. Nathan says:

    Breaking news: Microsoft blogger releases name of next version of the Windows operating system: Windows Q.

    Hopefully it involves muppets.

  18. ERock says:

    Does the blog software automatically mark up those buttons in the text? It looks really nice but I sure hope Raymond didn't have to do that by hand.

  19. That reminded me of cycling through NLM screens on a NetWare console back in the 4.11 days … ahh, nostalgia. (3.12 in a lot of places, in fact, although I had a 4.11 machine to support our trial deployment of Windows NT 4 on the desktop…) Presumably the function was left in Windows 95 because it was impossible to be certain nothing relied on it somewhere, and safer to leave it in place for the time being … and of course a few seconds of web searching now brings up pages where people are indeed talking about using it, so no chance of safely removing it now.

    @ERock: I imagine Raymond used a regex, or other search and replace method, to mark them all up in a few seconds at the end of editing.

    [I wish! It actually took a lot of iteration to get something that looked right. -Raymond]
  20. cheong00 says:

    @Grijan: I used DOSShell back for this then, although my uncle had installed Win3.1 for me, it's considered a bit too heavy for my 4MB RAM machine. Usually the only Windows programs I run at that time were winmine.exe, sol.exe, plus Turbo Pascal for Windows 1.5 for homework.

  21. caf says:

    This raises more interesting questions: like "Does this mean that the virtual machine manager had to keep track of the current windows and their z-order?" and "How did the virtual machine manager call Windows functions, anyway – was there a stub Windows process running to perform these calls?"

  22. Nitpicker says:

    "Not true, but true enough. Don't make me bring back the Nitpicker's Corner. "

    Why are you threatening us with the Nitpicker's Corner for asking about this issue (…/10013609.aspx) instead of explaining it once and linking it everywhere?

    [Because it is irrelevant to the history of SwitchToThisWindow. -Raymond]
  23. Neil says:

    Bonus bonus chatter:

    As far as I was able to tell some time ago when I looked at it, SwitchToThisWindow basically calls SetForegroundWindow.

    If you pass TRUE as the restore flag, then it deiconises the window. (But it doesn't change a maximised window.)

    If you pass FALSE as the restore flag, then the previous foreground window gets sent to the back of the z-order. However the window does not lose its topmost state. (I don't think there's an API that does this for topmost windows.)

  24. KooKiz says:

    Wow, I don't remember this text-mode task switcher one bit. No luck with Google either. If someone manages to find a screenshot, please share it!

  25. xpclient says:

    @KooKiz, the text mode switcher looked like this:…/textmodeswitcher.png.

  26. ChuckOp says:

    I love that you used the CSS system colors for the <kbd> styles.  That feature was something I really pushed for in IE (3 or 4, I forget) along with Scott Issacs and Chris Wilson.

  27. JustSomeGuy says:

    Mike Caron stated:

    "That's really interesting. I don't remember the text-version of Alt-Tab back when I used Windows 3.1. Then again, I probably didn't know about Alt-Tab way back then either!"

    From memory (and it may not be perfect memory – there's been quite a lot of alcohol ingested since those days), you wouldn't have seen the text interface unless you were in full-screen DOS mode. And, if you *were* in that mode, I don't think there was any other way back to Windows so you would have *had* to know about it.

    Standard disclaimers: I've been wrong before, I'll be wrong again. Just ask my wife :-)

  28. Gabe says:

    JustSomeGuy: The way most people left full screen mode was probably just by exiting the DOS program they were running. Alt-Tab has always been sort of a power-user feature.

  29. cheong00 says:

    @Gabe: I remember that once upon a time, I decided the proper way to run fullscreen DOS program in Windows is to write a stub program that calls ExitWindowExec()…/147806 . I lost track of how this function becomes nowadays, a brief search seems to show that people abuse it as a way to restart Windows.

    Don't know what it happens to later Windows now, my company machine is still using WinXP so I can't check.

  30. Mick says:

    I'm a bit late to the party but I'm glad Nick and I inspired such an enjoyable post. Thank you, Raymond!

    The bonus chatter is also much appreciated.

  31. Yuhong Bao says:

    @cheong00: ExitWindowsExec is not part of Win32 at all.

  32. Gabe says:

    cheong: It seems that people now only abuse ExitWindowsExec, they abuse RUNDLL to do so! As Yuhong suggests, of course, that's only on Win9x.

  33. cheong00 says:

    @Yuhong Bao: Oh, I forgot. So it must have been dropped then.

    @Gabe: I'm using WinXP SP3 here, and using a HEX editor seems to find out that the function ExitWindowsExec() still lives in USER.EXE (a 16-bit dll).

  34. Yuhong Bao says:

    cheong00: But plain 16-bit rundll (NOT rundll32) was never shipped with any version of NT-based Windows.

  35. Scott says:

    Wow, this was a blast from the past I only barely remember. It's been so long since I've used it. I remember now being vaguely annoyed that it worked differently but it makes sense.

    @xpclient: Interseting, on my Windows for Workgroups 3.11 the program name "title bar" is blue. Video driver differences?

  36. 640k says:

    Usually Raymond rather remove kernel functions than document them for competitors.…/361033.aspx

Comments are closed.