TONT 42473 如果某个import无法解析,为什么Win32就将模块加载视作失败?

台面下发生的事情。

原文链接:https://blogs.msdn.microsoft.com/oldnewthing/20030916-00/?p=42473

Because we tried it the other way and it was much worse.

因为我们尝试过其它方式,结果事态变得更糟糕了。

In 16-bit Windows, a module that didn’t satisfy all its imports would still load. As long as you didn’t call a missing import, you were fine. If you did try to call a missing import, you crashed pretty spectacularly with the dreaded Unrecoverable Application Error dialog.

在16位Windows环境下,未能满足所有import的模块仍可以加载。只要你的模块不去call不存在的import项,就天下太平。如果真的这么做了(Call了一个没能正常import的方法),那应用程序将会伴着两头尖尖这么大个的『严重应用程序错误』(Unrecoverable Application Error)对话框崩溃到连渣都不剩。

The Win32 folks decided that this was a bad design, because often people would take Fred App, designed for Windows 3.1, and run it on Windows 3.0, and it would run great for about an hour, at which point Fred App would call a function that was available only in Windows 3.1 (like, say, GetSaveFileName) and crash as a result.

Win32的开发组们认定这个设计很糟糕,因为有一种情况经常发生:用户把为Windows 3.1设计的Fred App跑在Windows 3.0上,并且最初运行的还不错,直到Fred App终于call了一个只在Windows 3.1上才有的方法(例如,GetSaveFileName),然后就崩掉了。

So the Win32 folks decided that if an import could not be resolved, the app should fail loading. If the makers of Fred App wanted to run on Windows 3.0 after all, they could indicate this by using GetProcAddress explicitly. Because if you have to call GetProcAddress explicitly, it’ll probably occur to you to check the return value.

因此,Win32的开发人员决定,如果有未能解析的import,那么应用程序就会加载失败。如果Fred App的开发公司无论如何都想让它们的程序跑在Windows 3.0上的话,可以通过明确调用GetProcAddress来指示操作系统这一点。因为,如果明确call了GetProcAddress,那么检查返回值的责任就落到了你的身上。

This issue comes up occasionally when people wish out loud, “Gosh, there should be a way I could mark an import as ‘optional’ – if it couldn’t bind, the load should not fail. It would be the app’s responsibility to verify that the bind succeeded before calling it.” These people are unwittingly asking for history to repeat itself.

偶尔还是会有人大喊着,“我去,应该有种方法可以指定某个import是『可选的』,如果这个可选import不能正确解析的话,整个应用程序的加载不该失败,然后应用程序在call这个绑定前有责任检查(方法相关的import)是否成功了。”这么说的人无非是在不经意间两次踏入同一条河流了而已。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

 剩余字数 ( Characters available )

Your comment will be available after auditing.
您的评论将在通过审核后显示。

Please DO NOT add any links in your comment, otherwise it would be identified as SPAM automatically and never be audited.
请不要在评论中插入任何链接,否则将被自动归类为垃圾评论,且永远不会被提交给博主进行复审。

*