TONT 36183 窗体不是便宜的对象

原文链接:https://devblogs.microsoft.com/oldnewthing/20050315-00/?p=36183

(译注:我深知这一篇的翻译质量应该很差,看不下去的话请直接跳过……)

Although Windows is centered around, well, windows, a window itself is not a cheap object. What’s more, the tight memory constraints of systems of 1985 forced various design decisions.

尽管 Windows 是围绕着窗体(windows)设计的,不过窗体并不是便宜大把的对象。此外,1985年硬件配置中紧俏的内存限制催生了很多被迫的设计决定。

Let’s take for example the design of the list box control. In a modern design, you might design the list box control as accepting a list of child windows, each of which represents an entry in the list. A list box with 20,000 items would have 20,000 child windows.

以 Listbox 控件的设计为例。在如今的设计理念中,你可能会将其设计为包含一列子窗体的控件,每个子窗体代表列表中的一个条目。一个拥有20000项条目的 Listbox 就会有20000个子窗体。

That would have been completely laughable in 1985.

这种设计在1985年会让人笑掉大牙。

Recall that Windows was built around a 16-bit processor. Window handles were 16-bit values and internally were just near pointers into a 64K heap. A window object was 88 bytes (I counted), which means that you could squeeze in a maximum of 700 or so before you ran out of memory. What’s more, menus hung out in this same 64K heap, so the actual limit was much lower.

回忆一下,当时 Windows 是围绕16位处理器设计的。窗体句柄是一系列16位的值,在内部则表现为一个 64K 堆中相邻的指针。一个窗体对象要用掉88个字节(我数过了),这就意味着你最多能在内存里塞下700个窗体,再塞就要内存溢出了。另外,菜单也在同一个 64K 堆中,故而实际的上限可能会更低。

Even if the window manager internally used a heap larger than 64K (which Windows 95 did), 20,000 windows comes out to over 1.5MB. Since the 8086 had a maximum address space of 1MB, even if you devoted every single byte of memory to window objects, you’d still not have enough memory.

即便窗体管理器在内部使用的是一个大于 64K 的堆(Windows 95即是如此),20000个窗体也要消耗将近 1.5MB 的空间。由于 8086 处理器的地址空间上限是 1MB,就算你将内存的每一个字节都贡献给窗体对象使用,还是没有足够的内存可以用。

Furthermore, making each list box item a window means that every list box would be a variable-height list box, which carries with it the complexity of managing a container with variable-height items. This goes against two general principles of API design: (1) simple things should be simple, and (2) “pay-for-play”, that if you are doing the simple thing, you shouldn’t have to pay the cost of the complex thing.

除此之外,让每一个 Listbox 的项目都作为一个窗体,意味着每个 Listbox 都会是高度可变的,这就带来了管理包含着可变高度项目的容器的复杂性。如此的设计违背了 API 设计的两项基本原则:(1)简单的事物就应该简单,以及(2)『办多少事花多少钱』,也就是如果事情本身就很简单,便不应当为复杂的事情付出代价。

Filling a list box with actual windows also would have made the “virtual list box” design significantly trickier. With the current design, you can say, “There are a million items” without actually having to create them.

将一个 Listbox 填满事实意义上的窗体,也会将这个『虚拟 Listbox』的设计变得陡然复杂起来。按照其目前的设计,你可以声明『列表中有一百万项』,但不必实际去创建这么多项目。

(This is also why the window space is divided into “client” and “non-client” areas rather than making the non-client area consist of little child windows.)

(这也是为什么窗体控件被分割为『工作区』和『非工作区』,而不是让非工作区也填满了小小的子窗体的缘故。)

To maintain compatibility with 16-bit Windows programs (which still run on Windows XP thanks to the WOW layer), there cannot be more than 65536 window handles in the system, because any more than that would prevent 16-bit programs from being able to talk meaningfully about windows. (Once you create your 65537’th window, there will be two windows with the same 16-bit handle value, thanks to the pigeonhole principle.)

为了维持与16位Windows应用程序的兼容性(鉴于WOW(译注:Windows-On-Windows,在新版Windows上为面向旧版系统设计的应用程序提供有限兼容性的兼容层,此处为32位系统兼容16位应用程序)层,这些程序在 Windows XP 上仍然可用),系统中的窗体句柄不能超过65536个,因为超过这个限制会阻止16位应用程序与系统对窗体进行有意义的沟通。(一旦创建了第65537个窗体,就会出现具有相同句柄值的两个窗体——感谢鸽巢原理(译注:又叫抽屉原理,指如果尝试将n+1个元素放进n个集合中,那么必然有一个集合中包含2个元素)的存在。)

(And yes, 16/32-bit interoperability is still important even today.)

(另外没错,16/32位应用程序的交互操作性,时至今日仍然很重要。)

With a limit of 65536 window handles, your directory with 100,000 files in it would be in serious trouble.

鉴于65535个窗体句柄上限的存在,你那拥有10万个文件的目录(如果按照前面那样嵌套小窗体的设计)就会有大麻烦了。

The cost of a window object has grown over time, as new features get added to the window manager. Today it’s even heftier than the svelte 88 bytes of yesteryear. It is to your advantage not to create more windows than necessary.

随着新功能不断加入窗体管理器,窗体对象的成本也在不断攀升。如今窗体数据的健壮程度已与当年那个苗条的88字节大小不可同日而语,而不要创建多于必要的窗体的责任就落在了你的肩上。

If your application design has you creating thousands of windows for sub-objects, you should consider moving to a windowless model, like Internet Explorer, Word, list boxes, treeview, listview, and even our scrollbar sample program. By going windowless, you shed the system overhead of a full window handle, with all the baggage that comes with it. Since window handles are visible to all processes, there is a lot of overhead associated with centrally managing the window list. If you go windowless, then the only program that can access your content is you. You don’t have to worry about marshalling, cross-process synchronization, Unicode/ANSI translation, external subclassing, hooks… And you can use a gigabyte of memory to keep track of your windowless data if that’s what you want, since your windowless controls don’t affect any other processes. The fact that window handles are accessible to other processes imposes a practical limit on how many of them can be created without impacting the system as a whole.

如果你的程序设计需要你创建成千上万个用于内部对象的窗体,你应当考虑迁移到无窗体的模型,就像 Internet Explorer、Word、Listbox、TreeView、ListView,甚至我们的滚动条示例程序那样。通过使用无窗体设计,你的程序向系统展示的就只有一个完整的窗体句柄,里面包含着所有大大小小的元素。由于窗体句柄是对所有进程可见的,对于集中管理的窗体列表来说负担是很重的。如果采用无窗体设计,那么唯一可以访问窗体中内容的就是你自己。你不必担心 Marshalling、跨进程同步、Unicode/ANSI 转换、外部子类、钩子…… 等等的麻烦事。另外如果你愿意,动用一个G的内存来跟踪你的无窗体模型下的内容也是刻意的,因为你的无窗体模型中的控件不会影响其它进程。由于窗体句柄对其他进程是可见的,也就对可以创建多少个窗体句柄而不会对系统整体产生重大影响这一事实施加了影响。

I believe that WinFX uses the “everything on the screen is an element” model. It is my understanding that they’ve built a windowless framework so you don’t have to. (I’m not sure about this, though, not being a WinFX person myself.)

我相信 WinFX 利用了『屏幕上的一切都是元素』这种模型,我的理解是他们构建了一个无窗体框架,以便你不用再从头造一遍轮子。(尽管对此我并不确定,毕竟我不是相关人士。)

TONT 36363 两则有关 Windows XP 的『Comments?』按钮的回忆

原文链接:https://devblogs.microsoft.com/oldnewthing/20050225-00/?p=36363

In beta versions of Windows XP, there was special code in the window manager to give every window a link in the upper right corner called “Comments?” which if clicked on displayed a dialog that allowed you to submit feedback to Microsoft about that window.

在 Windows XP 的beta 版本中,窗口管理器里有一段特别设计的代码,用来给每个窗口的右上角添加一个写作『Comments?』(有意见?)的链接,点击它会显示一个对话框,用于向微软提交关于那个窗口的反馈意见。

Since this was a beta release, there was no anonymity when you submitted feedback. (You signed away your anonymity when you agreed to the special beta license agreement and typed in your beta ID number.) Yet we got more than one feedback submission that begin, “Hi, I pirated this copy of Windows XP, and here’s some feedback.”

鉴于这是 beta 版,提交反馈时是没有什么匿名性可言的。(当你签署那张特别的 beta 授权协议并录入了你的 beta ID 时,你就已经让出让了你的匿名权。)不过我们还是收到了不止一份反馈,开头就是:『嗨,我这份 Windows XP 是盗版的,以下是我的反馈。』

In its initial incarnation, the word in the title bar was “Lame”, but people with a weaker sense of humor changed it to the less confrontational “Comments?”. The name “Lame” came from a recurring sketch on local comedy show Almost Live! called “The Lame List, or What’s Weak This Week (brought to you with the help of Seattle’s Heavy Metal community)”.

一开始的时候,标题栏里那个链接写的是『Lame』(跛的,瘸的),不过有个没太有幽默感的人把它换成了比较不具有挑衅性的『Comments?』。(译注:最后一句实在没看懂,但应该不影响对全文的理解,望看懂的朋友赐教。)

TONT 36483 为什么最小化的窗口具有形式上的160×31的尺寸?

原文链接:https://devblogs.microsoft.com/oldnewthing/20050210-00/?p=36483

We discussed a few months ago the issue of where windows minimized to before the taskbar was invented. In the modern taskbar world, why do minimized windows have an apparent size of 160×31?

几个月前,我们讨论过在任务栏发明之前的最小化窗口的问题。那么在如今任务栏一统天下的时代,为什么最小化的窗口会有一个形式上的160×31的尺寸呢?

The size isn’t just apparent. That’s really their size. You can see them, for example, if you fire up a program that uses the Multiple Document Interface.

这个尺寸不光是形式上的,实际上也是如此。你可以通过一些方式观察到这一点,例如启动一个食用了多文档界面(MDI)的程序。

(译注:原本这里有一张图片,但已经失效,请运用你的想象力)

Observe the appearance of the window “Book1”. This is a minimized window (though minimized to its MDI host rather than to the desktop). With the introduction of Windows Explorer, which put files on the desktop in the form of icons, it became necessary to change the appearance of minimized windows in order to avoid confusing a minimized program icon from a desktop icon. A minimized program, therefore, took the form of a miniature title bar.

观察这个叫『Book1』的窗口的外观。这是一个最小化的窗口(尽管是最小化到其MDI顶层窗口而不是桌面)。在引入了Windows资源管理器之后,桌面上允许以图标的形式放置文件了,修改最小化窗口的形式便变得有必要起来,以防人们将最小化的程序图标与文件图标相混淆(译注:Windows 95之前版本的Windows没有任务栏,最小化的程序会缩小为『桌面』上的一个图标,这里的桌面指的是『程序管理器』背后的整个屏幕空间,与Windows 95之后的『桌面』并不是同一个概念)。由此,最小化的程序使用了缩小的标题栏的形式。

The programming interface to minimized windows remained the same, for compatibility reasons. (And please let’s just agree to disagree on whether backwards compatibility is a good thing or not.) That’s why the function to tell whether a window is minimized continues to be called IsIconic, the message you receive when someone tries to restore a minimized program is still called WM_QUERYOPEN, and the OpenIcon function can still be used to “open” a minimized “icon”. All even though minimized windows haven’t looked like icons for nearly ten years.

面向最小化窗口的编程接口(与旧版Windows)保持了一致,这是出于兼容性的考虑。(至于向下兼容是一件好事还是坏事,请保持见仁见智的态度。)这就是为什么用来判断一个窗口是否最小化的方法名延续了 IsIconic 的称呼:当用户尝试复原最小化的程序时,程序获得信息仍然叫 WM_QUERYOPEN,而 OpenIcon 方法仍然可以用来『打开』一个最小化的『图标』,尽管保留了上述这些特性的同时,最小化的窗口已经跟图标的长相大相径庭将近十年了。

The OpenIcon function is just an old-fashioned way of saying ShowWindow(hwnd, SW_NORMAL), in the same way that the CloseWindow function (dating back to Windows 1.0) is an extremely old-fashioned way of saying ShowWindow(hwnd, SW_MINIMIZE).

OpenIcon 方法不过是 ShowWindow(hwnd, SW_NORMAL) 的一种旧式叫法,与此相同的还有 CloseWindow(可以追溯到 Windows 1.0)也只是 ShowWindow(hwnd, SW_MINIMIZE) 相对应的超级老的一种叫法而已。

TONT 36513 为什么 DS_SHELLFONT = DS_FIXEDSYS | DS_SETFONT?

原文链接:https://devblogs.microsoft.com/oldnewthing/20050207-00/?p=36513

You may have noticed that the numerical value of the DS_SHELLFONT flag is equal to DS_FIXEDSYS | DS_SETFONT.

你可能曾注意到 DS_SHELLFONT 这个 flag 的数字值是与 DS_FIXEDSYS | DS_SETFONT 相同的。

#define DS_SETFONT 0x40L /* 用于对话框控件的用户指定字体 */

#define DS_FIXEDSYS 0x0008L

#define DS_SHELLFONT (DS_SETFONT | DS_FIXEDSYS)

Surely that isn’t a coincidence.

这当然不是巧合。

The value of the DS_SHELLFONT flag was chosen so that older operating systems (Windows 95, 98, NT 4) would accept the flag while nevertheless ignoring it.

DS_SHELLFONT 这个 flag 的值这样设计,是为了让旧版操作系统(Windows 95、98、NT 4)可以接受这个值的同时忽略它。

This allowed people to write a single program that got the “Windows 2000” look when running on Windows 2000 and got the “classic” look when running on older systems. (If you make people have to write two versions of their program, one that runs on all systems and one that runs only on the newer system and looks slightly cooler, they will usually not bother writing the second one.)

这样的设计,使得人们可以在编写程序时,一次性做到程序在 Windows 2000 下运行时获得『Windows 2000 样式』的同时,在旧版系统上运行时获得『经典样式』。(如果你非要人们编写两个版本的程序,一个可以在所有版本的系统上运行,但另一个只是在新版系统上运行时看上去漂亮一些的话,估计人们根本就不会考虑去写后一个版本。)

The DS_FIXEDSYS flag met these conditions. Older systems accepted the flag since it was indeed a valid flag, but they also ignored it because the DS_SETFONT flag takes precedence.

DS_FIXEDSYS 这个 flag 就可以同时适应这两种情况。旧版系统可以接受这个值,因为它的确是个有效的 flag,但同时也会忽略它,因为 DS_SETFONT 这个 flag 会被优先使用。

This is one of those backwards-compatibility exercises: How do you design something so that it is possible to write one program that gets the new features on new systems while at the same time degrading gracefully on old systems?

这就是向下兼容措施的其中之一:如何设计一种机制,使得只需撰写一个版本的程序,便可以在新系统上获得新功能,同时又在旧系统上可以优雅地降级呢?

TONT 36543 Windows PowerToys 的故事

原文链接:https://devblogs.microsoft.com/oldnewthing/20050202-00/?p=36543

During the development of Windows 95, as with the development of any project, the people working on the project write side programs to test the features they are adding or to prototype a feature. After Windows 95 shipped, some of those programs were collected into the first edition of the Windows 95 Power Toys.

在 Windows 95 的开发过程中,与其他的项目一样,开发人员总会编写一些小工具,用来测试讲要加入的功能,或者为某个功能打造原型。Windows 95 发布之后,其中一些小工具被收录进了第一版的 Windows 95 PowerToys。

As I recall, the first edition contained the following toys:

根据我的回忆,第一版 PowerToys 里有以下这些玩具:

CabView

This was a handy internal tool which also served as a test of the shell folder design.

一个很有帮助的内部工具,同时也是对系统外壳文件夹设计的测试。

CDAutoPlay, DeskMenu, FlexiCD, QuickRes

These were side toys originally written by shell developers for their own personal use.

这些是系统外壳开发人员写来自用的小工具。

Command Prompt Here, Explore From Here(从此处打开命令提示符、从此处打开资源管理器)

These were proof-of-concept toys which tested the shell command extension design.

这些是对系统外壳命令扩展设计的概念论证。

Round Clock(圆形钟)

This was a program to test regional windows.

这是用来测试区域化窗口的程序。

Shortcut Target Menu

This was a feature idea that didn’t quite make it.

这是一个新功能的点子,不过没能实现。

I wasn’t around when the decision was made to package these toys up and ship them, so I don’t know what the rule was for deciding what was PowerToy-worthy and what wasn’t. Nor do I know where the name PowerToy came from. (Probably somebody just made it up because it sounded neat.)

以上这些工具打包发布的时候我并没有参与,所以我也不知道入选或不入选 PowerToy 的标准是什么,也不知道 PowerToy 这个名字是怎么来的(可能有人觉得这样念起来感觉不错,所以就这么愉快的决定了)。

Upon the enormous success of the PowerToys, a second edition was developed. This time, people knew that they were writing a PowerToy, as opposed to the first edition of the PowerToys which was merely cobbled together from stuff lying around. The second edition of the Windows 95 PowerToys added FindX, Send To X, the Telephony Locator Selector, XMouse, and Tweak UI.
Later, the kernel team released their own set of toys, known as the Windows 95 Kernel Toys. Alas, the original blurb text is not on the Microsoft downloads site, but here’s an archived copy. (In reality, it was I who wrote all of the Kernel Toys, except for the Time Zone Editor, which came from the Windows NT Resource Kit. I also wrote the somewhat whimsical original blurb.)

鉴于 PowerToys 的良好反响,这套工具又有了第二版。这一次,开发者们知道自己是在专门开发 PowerToy,而不是像第一版的 PowerToys 那样仅仅是将散落在各处的东西拼凑在一起。Windows 95 PowerToys 第二版增加了 FindX、Send To X(发送到X)、the Telephony Locator Selector(电话位置选择器,用于修改 Windows 95 模拟电话拨号的『所在位置』设置——译注)、XMouse,以及 Tweak UI。后来,内核开发组发布了他们自己的套件,并起名为 Windows 95 Kernel Toys。不巧的是,原始的那些絮絮叨叨的介绍文字已经不在微软的下载站上了,不过这里有一份备份。(实际上,除了时区编辑器之外,是我撰写了所有的 Kernel Toys,而时区编辑器是从 Windows NT Resource Kit 中拿来的。那个冗长而古怪的介绍文字也是我的作品。)

This was all back in the day when it was easy to put up something for download. No digital signatures, no virus checking, no paperwork. Just throw it up there and watch what happens. Today, things are very different. Putting something up for download is a complicated process with forms to fill out in triplicate and dark rooms with card readers. I wouldn’t be surprised if an abandoned salt mine in Montana were somehow involved.

那是个将什么东西发布出来供人下载十分简单易行的年代。没有什么数字签名,没有什么病毒检测,也没有什么文件要签署,只要把它传上去,就可以观察会发生什么化学反应了。现如今,事情已经变得完全不同,要把什么东西发出来供人下载变成了一个复杂的过程,要填一大堆一式三份的表格,还得在带着门禁卡的小黑屋之类的地方。这个过程中就算包含了蒙大拿废弃的盐矿我也不会觉得奇怪。(译注:这句没看懂,如有知道的读者请帮忙解释一下。)

Nowadays, every team at Microsoft seems to have their own PowerToys, trading on the good name of the Windows shell team who invented the whole PowerToys idea. (As far as I can tell, we don’t get any royalties from other divisions calling their toys “PowerToys”.) A quick check reveals the following PowerToys available for download from Microsoft; I may have missed some.

现在,每个微软的团队似乎都有自己的 PowerToys 套件了,这些套件也利用了发明了 PowerToys  这个主意的 Windows 系统外壳团队的名声。(据我所知,其他团队把他们的套件叫做 PowerToys 的时候,我们可一分钱版税都没收到。)快速地浏览一遍让我列出了可以从微软网站上下载到的 PowerToys,或许也有些遗漏了。

  • Microsoft PowerToys for Windows XP Tablet PC Edition [link fixed 11am]
  • PowerToys for the Pocket PC
  • PowerToys Fun Pack
  • PowerToys for Microsoft Office OneNote 2003
  • Microsoft PowerToys for Windows XP Media Center Edition 2004
  • PowerToys for Windows Media Player for Windows XP
  • Windows XP Creativity Fun Pack PowerToys Wallpaper Changer

(译注:以上原文链接已全部失效,故未再做链接,有兴趣可以去看原文)

(Plus, of course, the Windows XP PowerToys, which does come from the shell team. The Internet Explorer team originally called their stuff PowerToys, but they later changed the name to Web Accessories, perhaps to avoid the very confusion I’m discussing here.)

(另外,当然还应该算上 Windows XP PowerToys,也的确是由(Windows)系统外壳团队开发的。Internet Explorer 团队原本也把他们的套件叫做 PowerToys,不过后来改名叫 Web Accessories(网络附件)了,可能就是为了避免这里所说的混乱局面。)

What’s frustrating is that since they are all called “PowerToys”, questions about them tend to go to the shell team, since we are the ones who invented PowerToys. We frequently have to reply, “Oh, no, you’re having a problem with the XYZ PowerToys, not the classic Windows PowerToys. We’re the folks who do the classic Windows PowerToys.”

令人沮丧的是,因为这些套件都叫 PowerToys,有关各种(不同团队的)套件的问题常常会跑到系统外壳团队那里去,毕竟我们是发明了 PowerToys 的人。我们常常需要回复说,『哦,不是的,你的问题与 XYZ PowerToys 有关,而不是传统的 Windows PowerToys,我们是制作传统的 Windows PowerToys 的那批人。』

Even the blog name “PowerToys” has been co-opted by the Visual Studio team to promote their Powertoys for Visual Studio 2003.

甚至连『PowerToys』这个博客名称都被 Visual Studio 团队指派用来宣传 Visual Studio 2003 的 PowerToys 了。

Some people claim that Tweak UI was written because Microsoft got tired of responding to customer complaints. I don’t know where they got that from. Tweak UI was written because I felt like writing it.

有些人声称开发 Tweak UI 的原因是微软已经受够了回复客户的抱怨,我是不知道他们是从哪听来这种说法的。我写 Tweak UI 只是因为我想这么做而已。

That page also says that sometimes PowerToys vanish without warning. That’s true. A few years ago, all the Windows XP PowerToys were taken down so they could be given a security review. Some of them didn’t survive and didn’t come back. Other times, a PowerToy will be pulled because a serious bug was found. Since PowerToys are spare-time projects, it can take a very long time for a bug to get fixed, tested, and re-published. For example, the HTML Slide Show Wizard was pulled after a (somewhat obscure) data-loss bug was found. Fixing the bug itself took just a few days, but testing and filling out all the associated paperwork took six months.

同一篇文章里(译注:即上文中提到 Tweak UI 是微软懒得再回复客户问题的那篇博文,因原文链接已失效,未予链接)还提到有些 PowerToys 套件在未予告知的情况下就消失了,的确有这么回事。几年前,整个 Windows XP PowerToys 都下线了,以便接受安全评估,后来套件中的一些通过了评估,另一些则再也没能回来。其它场合下,某个 PowerToys 套件被撤下是因为发现了严重的 bug。由于 PowerToys 都是在空余时间制作的作品,可能需要很长的时间才能修复 bug、通过测试,然后重新上线。例如,一个叫 HTML 幻灯片向导的组件曾被撤了下来,因为其中发现了一个(隐藏得比较深的)数据丢失漏洞。修好 bug只花了几天时间,但跑测试和填写相关的文书则花了六个月。

There’s no moral to this story. Just a quick history lesson.

上面的故事没有什么寓意,只是讲点小历史而已。

TONT 36673 保持错误代码向下兼容的重要性

原文链接:https://devblogs.microsoft.com/oldnewthing/20050118-00/?p=36673

I remember a bug report that came on in an old MS-DOS program (from a company that is still in business so don’t ask me to identify them) that attempted to open the file “”. That’s the file with no name.

我记得有一个bug报告,是关于一个老旧的 MS-DOS 程序(开发这个程序的公司目前仍在存续中,所以不要问我具体是哪家公司)尝试打开文件“”,也就是一个没有名字的文件。

This returned error 2 (file not found). But the program didn’t check the error code and though that 2 was the file handle. It then began writing data to handle 2, which ended up going to the screen because handle 2 is the standard error handle, which by default goes to the screen.

这样做会使系统报告错误代码2(文件未找到),但程序没有检查错误代码,以为2就是文件句柄,然后就会开始向句柄2填充数据,而数据会显示在屏幕上,而这是因为句柄2是标准错误输出句柄,其默认行为就是输出到屏幕上。

It so happened that this program wanted to print the message to the screen anyway.

碰巧这个程序要做的就是向屏幕输出消息。

In other words, this program worked completely by accident.

换句话说,这个程序只是撞了大运正常工作了。

Due to various changes to the installable file system in Windows 95, the error code for attempting to open the null file changed from 2 (file not found) to 3 (path not found) as a side-effect.
Watch what happens.

在 Windows 95 可以安装在其上的文件系统设计的几次变动中,其中一个副作用是:尝试打开一个不存在的文件回报的错误代码从2(文件不存在)变成了3(找不到路径)。现在来看看会发生什么事。

The program tries to open the file “”. Now it gets error 3 back. It mistakenly treats the 3 as a file handle and writes to it.

程序尝试打开文件“”(空文件名)。现在它获得了错误代码3。程序照旧误打误撞将3作为文件句柄,并开始向其中写入数据。

What is handle 3?

那么句柄3是什么呢?

The standard MS-DOS file handles are as follows:

标准的 MS-DOS 文件句柄如下所示:

句柄 名称 含义
0 stdin 标准输入设备
1 stdout 标准输出设备
2 stderr 标准错误输出
3 stdaux 标准辅助设备(串口)
4 stdprn 标准打印机

What happens when the program writes to handle 3?

当程序尝试向句柄3写入时会发生什么呢?

It tries to write to the serial port.

会尝试向串口写数据。

Most computers don’t have anything hooked up to the serial port. The write hangs.

大多数计算机的串口上什么也没连,所以写操作挂起了。

Result: Dead program.

结果就是:程序死掉了。

The file system folks had to tweak their parameter validation so they returned error 2 in this case.

文件系统开发组对参数校验做了些调整,使其在这种情况下返回错误代码2(来解决这个问题)。

TONT 36683 MS-DOS 是如何报告错误代码的?

原文链接:https://devblogs.microsoft.com/oldnewthing/20050117-00/?p=36683

The old MS-DOS function calls (ah, int 21h), typically indicated error by returning with carry set and putting the error code in the AX register. These error codes will look awfully familiar today: They are the same error codes that Windows uses. All the small-valued error codes like ERROR_FILE_NOT_FOUND go back to MS-DOS (and possibly even further back).

旧式的 MS-DOS 功能调用(啊,INT 21h)通常通过在返回中设置carry标志、并将错误代码放在AX寄存器中来表明发生了错误。这些错误代码即使今天看起来也极其眼熟,因为 Windows 也使用了相同的错误代码。所有这些由小小的数字代表的错误代码——如 ERROR_FILE_NOT_FOUND ——都可以追溯到 MS-DOS(并且可能更早)。

Error code numbers are a major compatibility problem, because you cannot easily add new error code numbers without breaking existing programs. For example, it became well-known that “The only errors that can be returned from a failed call to OpenFile are 3 (path not found), 4 (too many open files), and 5 (access denied).” If MS-DOS ever returned an error code not on that list, programs would crash because they used the error number as an index into a function table without doing a range check first. Returning a new error like 32 (sharing violation) meant that the programs would jump to a random address and die.

错误代码是一项主要的兼容性问题,因为你无法简单地增加新的错误代码,而不影响已有的应用程序。例如,广为人知的是『调用 OpenFile 且失败时,可能的返回只会是3(找不到路径)、4(打开的文件数已超出上限)或者5(拒绝访问)』。如果 MS-DOS 返回了一个不在这个列表上的错误代码,(第三方)程序们就会崩溃,因为这些程序将错误代码用作了函数列表的索引,甚至连边界检查都没做。返回一个新的错误代码(例如32)会让这些程序跳到一个随机的地址,然后炸掉。

More about error number compatibility next time.

下次有机会时,我们再来说有关错误代码兼容性的事。

When it became necessary to add new error codes, compatibility demanded that the error codes returned by the functions not change. Therefore, if a new type of error occurred (for example, a sharing violation), one of the previous “well-known” error codes was selected that had the most similar meaning and that was returned as the error code. (For “sharing violation”, the best match is probably “access denied”.) Programs which were “in the know” could call a new function called “get extended error” which returned one of the newfangled error codes (in this case, 32 for sharing violation).

等到增加新的错误代码变得有必要时,兼容性需求会要求函数返回的错误代码不能改变。因此,当某个新型的错误发生时(例如共享违例),会返回一个之前『最广为人知』且含义最为接近的的错误代码。(对于『共享违例』来说,最佳的匹配项是『拒绝访问』)。那些『知道内情』的(新)程序可以通过调用名为『获取扩展错误代码』的方法来获取那些『新奇』的错误代码(在前面的例子中,程序会获得32——共享违例)。

The “get extended error” function returned other pieces of information. It gave you an “error class” which gave you a vague idea of what type of problem it is (out of resources? physical media failure? system configuration error?), an “error locus” which told you what type of device caused the problem (floppy? serial? memory?), and what I found to be the most interesting meta-information, the “suggested action”. Suggested actions were things like “pause, then retry” (for temporary conditions), “ask user to re-enter input” (for example, file not found), or even “ask user for remedial action” (for example, check that the disk is properly inserted).

这个『获取扩展错误代码』方法还返回了其它的信息,它会给你返回一个『错误类』来通知你关于问题的大致类别(资源不足?媒体硬件损坏?系统设置出错?),一个『错误核心』来告知你导致错误发生的具体设备类型(软驱?串口?内存?),以及我认为最有趣的元信息部分——『建议操作』。『建议操作』会是类似『暂停,然后重试』(对于暂时性的问题来说),『要求用户重新提供输入』(例如找不到文件这类错误),甚至『要求用户实行补救措施』(例如检查磁盘是否正确插入了)等等。

The purpose of these meta-error values is to allow a program to recover when faced with an error code it doesn’t understand. You could at least follow the meta-data to have an idea of what type of error it was (error class), where the error occurred (error locus), and what you probably should do in response to it (suggested action).

这些有关错误的元数据有助于程序在面对一个其不了解的错误代码时,从错误中恢复过来。至少你可以从元数据所描述中,知晓出错的类型(错误类)、出错的所在(错误核心)以及面对错误时可能应该进行的操作(建议操作)。

Sadly, this type of rich error information was lost when 16-bit programming was abandoned. Now you get an error code or an exception and you’d better know what to do with it. For example, if you call some function and an error comes back, how do you know whether the error was a logic error in your program (using a handle after closing it, say) or was something that is externally-induced (for example, remote server timed out)? You don’t.

可惜的是,这种丰富的错误信息设计随着16位程序退出历史舞台被遗弃了。现在当你面对错误代码或异常信息时,你最好知道自己应该做什么。例如,如果你调用了某个方法,然后返回了一个错误,你如何知道这是你程序设计中的逻辑错误(例如在关闭某个句柄后又去使用它),还是某些外界因素的导致的(例如远程服务器超时)?你没法知道。

This is particularly gruesome for exception-based programming. When you catch an exception, you can’t tell by looking at it whether it’s something that genuinely should crash the program (due to an internal logic error – a null reference exception, for example) or something that does not betray any error in your program but was caused externally (connection failed, file not found, sharing violation).

这种情形在面对以异常为错误机制的编程时尤为可惧。当你捕获了一个异常时,你没有办法通过观察异常信息,来判断是什么地方真的让你的程序崩溃了(来自内部的逻辑设计错误,例如空引用异常等等),还是某些实际上与你的程序无关、而是某些外界因素导致的(例如连接失败、未找到文件、共享违例等等)。

TONT 36743 为什么\\不会触发自动完成、并列出网络上所有的计算机?

原文链接:https://devblogs.microsoft.com/oldnewthing/20050111-00/?p=36743

Wes Haggard wishes that \ would autocomplete to all the computers on the network. [Link fixed 10am.] An early beta of Windows 95 actually did something similar to this, showing all the computers on the network when you opened the Network Neighborhood folder. And the feature was quickly killed.

Wes Haggard 希望(在『运行』对话框或地址栏中)输入 \\ 时,自动完成功能可以列出网络上的所有计算机。Windows 95 的一个早期 beta 版本实际上有一个与此类似的功能,当你打开『网上邻居』文件夹时便列出网络上的所有计算机,然而这个功能很快就被砍掉了。

Why?

为什么呢?

Corporations with large networks were having conniptions because needlessly enumerating all the machines on the network can bring a large network to its knees. Think about all the times you type “\\”. Now imagine if every single time you did that, Explorer started enumerating all the machines on the network. And imagine how your network administrator would feel if their network traffic saturated with enumerations each time you did that.

拥有大型网络的企业对此大动肝火,因为毫无必要地枚举出网络上所有的计算机有将一个大型网络搞到跪的能力。想像一下每次你输入 \\ 的时候。然后再想像一下每次你这样做的时候,资源管理器都会开始枚举网络上所有的计算机。再想像一下每次你这样做时,网络上的巨额流量会让网管的脸有多难看。

Network administrators made it clear in no uncertain terms that having Windows casually enumerate all the machines on their LAN was totally unacceptable.

网管们非常清楚且毫不含糊地表示,让 Windows 随随便便就在局域网上枚举所有计算机是完全不可接受的事情。

The needs of the corporate environment are very different from those of the home network, and Windows needs to operate in both worlds.

企业环境的需求与家庭网络大相径庭,而 Windows 需要在两种环境下都能正常操作。

TONT 37003 追寻更加迅速的syscall陷阱

原文链接:https://devblogs.microsoft.com/oldnewthing/20041215-00/?p=37003

The performance of the syscall trap gets a lot of attention.

有关 syscall 陷阱的效率问题吸引了很多人的注意。

I was reminded of a meeting that took place between Intel and Microsoft over fifteen years ago. (Sadly, I was not myself at this meeting, so the story is second-hand.)

我想起了十五年前 Intel 和微软之间的一次会议。(很遗憾当时我没有亲自在场,所以接下来的故事是转述的。)

Since Microsoft is one of Intel’s biggest customers, their representatives often visit Microsoft to show off what their latest processor can do, lobby the kernel development team to support a new processor feature, and solicit feedback on what sort of features would be most useful to add.

鉴于微软是 Intel 最大的客户之一,Intel 的代表经常到访微软,炫耀他们最新款的处理器的能力,游说内核开发团队支持一项新的处理器功能,并且征求有关有意向添加到处理器中的、最有用的功能类别。

At this meeting, the Intel representatives asked, “So if you could ask for only one thing to be made faster, what would it be?”

在那次会议上,Intel 的代表问道,『如果只有一件事可以提速,你们希望是什么呢?』

Without hesitation, one of the lead kernel developers replied, “Speed up faulting on an invalid instruction.”

内核团队的一位领头开发者不假思索地回答道:『执行无效指令时的出错再快一点。』

The Intel half of the room burst out laughing. “Oh, you Microsoft engineers are so funny!” And so the meeting ended with a cute little joke.

会议室里 Intel 一侧的人们大笑起来,『哎呀,你们微软的工程师可真有意思!』会议在这个小而有趣的玩笑中收场了。

After returning to their labs, the Intel engineers ran profiles against the Windows kernel and lo and behold, they discovered that Windows spent a lot of its time dispatching invalid instruction exceptions. How absurd! Was the Microsoft engineer not kidding around after all?

等回到实验室之后,Intel 的工程师们对 Windows 的内核进行了测评,出乎意料地发现 Windows 花了大量的时间来调度无效的指令异常。这也太荒谬了吧!微软的那些工程师原来并不是在开玩笑吗?

No he wasn’t.

还真不是。

It so happens that on the 80386 chip of that era, the fastest way to get from V86-mode into kernel mode was to execute an invalid instruction! Consequently, Windows/386 used an invalid instruction as its syscall trap.

原来在那个时代的 80386 处理器上,从虚拟8086模式切换到内核模式最快的方法,正是执行一个无效的指令!因此,Windows/386 将无效指令作为了 syscall 的陷阱。

What’s the moral of this story? I’m not sure. Perhaps it’s that when you create something, you may find people using it in ways you had never considered.

至于这个故事教给我们的道理是什么,我并不太确定。大概是当你创造了一项事物时,你会发现人们会用你从未考虑过的方式去使用它。

TONT 37153 为什么 Windows 95 的定时器的运行频率是 55ms?

原文链接:https://devblogs.microsoft.com/oldnewthing/20041202-00/?p=37153

The story behind the 55ms timer tick rate goes all the way back to the original IBM PC BIOS. The original IBM PC used a 1.19MHz crystal, and 65536 cycles at 1.19MHz equals approximately 55ms. (More accurately, it was more like 1.19318MHz and 54.92ms.)

定时器的运行频率是 55ms 追根究底要回到原始的 IBM PC BIOS 上。最初的 IBM PC 使用了一颗 1.19MHz 的晶振,而 1.19MHz 上 65536 个时钟周期所需的时间大约就是 55ms。(更准确的说,应该是 1.19318 MHz 和 54.92ms。)

But that just pushes the question to another level. Why 1.19…MHz, then?

不过这样一解释只是将问题又推高了一个级别,为什么是 1.19 MHz 呢?

With that clock rate, 216 ticks equals approximately 3600 seconds, which is one hour. (If you do the math it’s more like 3599.59 seconds.) [Update: 4pm, change 232 to 216; what was I thinking?]

在这样的时钟频率下,216 个嘀嗒(tick)大约就是 3600 秒,也就是一小时。(精确一些的话,也可以说是3599.59 秒。)

What’s so special about one hour?

为什么『一个小时』这个周期那么特别呢?

The BIOS checked once an hour to see whether the clock has crossed midnight. When it did, it needed to increment the date. Making the hourly check happen precisely when a 16-bit tick count overflowed saved a few valuable bytes in the BIOS.

BIOS 每小时会检查一次系统时钟来确定是否跨越了午夜,当这种情况发生时,系统就会将日期向前推进一天。让这种检查机制发生在16位嘀嗒存储器溢出的时刻,可以在 BIOS 中节约宝贵的几个字节。

Another reason for the 1.19MHz clock speed was that it was exactly one quarter of the original CPU speed, namely 4.77MHz, which was in turn 4/3 times the NTSC color burst frequency of 3.5MHz. Recall that back in these days, personal computers sent their video output to a television set. Monitors were for the rich kids. Using a timer related to the video output signal saved a few dollars on the motherboard.

另一个采用 1.19MHz 时钟频率的原因是因为这个值正好是原始设计中 CPU 运行速度—— 4.77MHz ——的四分之一,而这正好又是 NTSC 制式的彩色信号频率的三分之四倍(译注:没有打错,4.77除以3.5约等于4除以3)。当年,个人电脑是将其视频信号输出到电视上的,那时候显示器是有钱人的玩具,而将定时器频率与视频信号关联起来则又在主板上省出了几美元的成本。

Calvin Hsia has another view of the story behind the 4.77MHz clock.

Calvin Hsia 提供了有关 4.77 MHz 时钟频率的另一个角度的故事。(译注:链接已失效)

(Penny-pinching was very common at this time. The Apple ][ had its own share of penny-saving hijinks.)

(那时候一分钱掰成两半花是很常见的事,Apple ][ 有其自己的省钱小妙招。)(译注:链接已失效)