从 UNIX 到 Win32 的迁移Porting from UNIX to Win32

将应用程序从 UNIX 迁移到 Windows 时,有以下几个选项:When migrating applications from UNIX to Windows, you have several options:

  • 使用 UNIX 库来将应用程序从 UNIX 迁移到 Win32Using UNIX libraries to port applications from UNIX to Win32

  • 在本机将应用程序从 UNIX 迁移到 Win32Porting applications from UNIX to Win32 natively

  • 使用 POSIX 子系统在 Windows 上运行 UNIX 应用程序Running UNIX applications on Windows using the POSIX subsystem

UNIX 库UNIX libraries

UNIX 程序员通常考虑使用类似于 UNIX 库的第三方库来将 UNIX 代码编译为 Win32 可执行文件。One option UNIX programmers normally consider is using third-party UNIX-like libraries to let their UNIX code compile as a Win32 executable. 有几个商用(和至少一个公共域)库可以执行此操作。Several commercial (and at least one public domain) libraries do this. 此选项可用于一些应用程序。This is an option for some applications. 这些迁移库的优点在于最大限度地减少了初始迁移工作量。The advantage of these porting libraries is that they minimize the initial porting effort. 对于有竞争力的软件产品来说,其主要缺点是应用程序的本机 Win32 端口通常速度更快并必然拥有更多功能。The main disadvantage, for a competitive software product, is that a native Win32 port of an application will generally be faster and will inevitably have more functionality. 如果应用程序需要执行 Win32 调用以从 Windows 中获取更多资源,则很难冲出其 UNIX 外壳。It can be awkward for the application to step outside of its UNIX shell if it needs to make Win32 calls to get more power from Windows.

下表为迁移和支持 UNIX 到 Visual C++ 的迁移提供了 Microsoft 和第三方资源:The following list provides Microsoft and third-party resources for porting and supporting UNIX migration to Visual C++:

UNIX 迁移指南UNIX Migration Guides

《UNIX 自定义应用程序迁移指南》为从 UNIX 到 Win32 环境的代码迁移提供了技术帮助。The UNIX Custom Application Migration Guide provides technical help on code migration from UNIX to the Win32 environment.

http://go.microsoft.com/fwlink/p/?linkid=95428http://go.microsoft.com/fwlink/p/?linkid=95428

《Unix 迁移项目指南》对《UNIX 自定义应用程序迁移指南》进行了补充,在从 UNIX 到 Win32 迁移大量项目方面提供了高级帮助。The Unix Migration Project Guide supplements the UNIX Custom Application Migration Guide by providing high-level help on migrating substantial projects from UNIX to Win32. 该指南针对项目迁移的各个阶段中要考虑的问题提供了相应建议。The Guide provides advice on issues to consider at each stage of project migration. 该指南可从以下地址下载:The Guide may be downloaded from:

http://go.microsoft.com/fwlink/p/?linkid=20012http://go.microsoft.com/fwlink/p/?linkid=20012

Microsoft Windows Services for UNIX (SFU)Microsoft Windows Services for UNIX (SFU)

Microsoft Windows Services for UNIX (SFU) 提供了一系列完整的用于将 Windows 集成到基于 UNIX 的现有环境的跨平台服务。Microsoft Windows Services for UNIX (SFU) provides a full range of cross-platform services for integrating Windows into existing UNIX-based environments. 面向 UNIX 的服务提供文件共享、远程访问和管理、密码同步、公共目录管理、一组常用的实用工具和一个外壳。Services for UNIX provides file sharing, remote access and administration, password synchronization, common directory management, a common set of utilities, and a shell.

Windows Services for UNIXWindows Services for UNIX

InteropSystems.comInteropSystems.com

http://www.interopsystems.com/http://www.interopsystems.com/

为公司提供从 UNIX 迁移到 Win32 的软件支持的第三方站点。A third party site for a company providing software supporting porting UNIX to Win32.

C++ Boost 网站C++ Boost Web Site

http://boost.sourceforge.net/regression-logs/http://boost.sourceforge.net/regression-logs/

http://boost.sourceforge.net/boost-build2/http://boost.sourceforge.net/boost-build2/

将 UNIX 应用程序直接迁移到 Win32Porting UNIX applications directly to Win32

另一种方法是将 UNIX 应用程序直接迁移到 Win32。Another option is porting UNIX applications directly to Win32. 借助 ANSI C/C++ 库和商用 C 编译器库,Win32 应用程序中提供了很多 UNIX 应用程序依赖的传统系统调用。Using ANSI C/C++ libraries, and commercial C compiler libraries, many of the traditional system calls relied on by UNIX applications are available in Win32 applications.

无需更改基于 stdio 的应用程序的输出模型,因为 Win32 控制台 API 会模拟 stdio 模型,而且存在使用 Win32 控制台 API 的 curses 版本。The output model of stdio-based applications does not need to be changed, since the Win32 console APIs mimic the stdio model, and versions of curses exist that use the Win32 console APIs. 有关详细信息,请参阅 SetConsoleCursorPositionFor more information, see SetConsoleCursorPosition.

基于 Berkeley 套接字的应用程序需要稍做更改,以便像 Win32 应用程序一样运行。Berkeley socket-based applications need very few changes to work as Win32 applications. Windows 套接字接口旨在通过 WinSock 规范的介绍部分中所述稍作更改,实现与 BSD 套接字的可移植性。The Windows Sockets interface was designed for portability with BSD sockets, with minimal changes that are noted in the introductory sections of the WinSock specification.

Windows 支持符合 DCE 的 RPC,因此基于 RPC 的应用程序便于使用。Windows supports DCE-compliant RPC, so RPC-based applications are easily usable. 请参阅 RPC 函数See RPC Functions.

其中的一个最大差异在于进程模型。One of the largest areas of difference is in the process model. UNIX 具有 fork;Win32 却没有。UNIX has fork; Win32 does not. 根据使用的 fork 和基本代码,Win32 可使用两种 API:CreateProcessCreateThreadDepending on the use of fork and the code base, Win32 has two APIs that can be used: CreateProcess and CreateThread. 可在 Win32 中修改其自行派生了多个副本的 UNIX 应用程序,使其拥有多个进程或一个具有多个线程的进程。A UNIX application that forks multiple copies of itself can be reworked in Win32 to have either multiple processes or a single process with multiple threads. 如果使用多个进程,则可使用多个 IPC 方法在进程之间进行通信(如果需要 fork 提供的功能,可能还可以将新进程的代码和数据更新为如父级一样)。If multiple processes are used, there are multiple methods of IPC that can be used to communicate between the processes (and perhaps to update the code and data of the new process to be like the parent, if the functionality that fork provides is needed). 有关 IPC 的详细信息,请参阅进程间通信For more on IPC, see Interprocess Communications.

Windows 和 UNIX 图形模型有很大差异。Windows and UNIX graphical models are very different. UNIX 使用 X 窗口系统 GUI,而 Windows 使用 GDI。UNIX uses the X Window System GUI, while Windows uses GDI. 尽管在概念上类似,但无法从 X API 简单映射到 GDI API。Though similar in concept, there is no simple mapping of the X API to the GDI API. 但是,OpenGL 支持可用于迁移基于 UNIX OpenGL 的应用程序。However, OpenGL support is available for migrating UNIX OpenGL-based applications. 还有针对 Windows 的 X 客户端和 X 服务器。And there are X clients and X servers for Windows. 请参阅设备上下文以获取 GDI 的相关信息。See Device Contexts for information on GDI.

基本 UNIX 应用程序(包括许多 CGI 应用程序)应轻松地迁移到 Windows 上运行的 Visual C++。Basic UNIX applications, including many CGI applications, should port easily to Visual C++ running on Windows. 诸如 openfopenreadwrite 等功能在 Visual C++ 运行时库中可用。Functions like open, fopen, read, write and others are available in the Visual C++ run-time library. 此外,C UNIX API 和 Win32 API 之间存在一对一映射:open 对应 CreateFileread 对应 ReadFilewrite 对应 WriteFileioctl 对应 DeviceIOControl,而 close 对应 CloseFile 等等。Also, there is a one-to-one mapping between C UNIX APIs and Win32 APIs: open to CreateFile, read to ReadFile, write to WriteFile, ioctl to DeviceIOControl, close to CloseFile, and so on.

Windows POSIX 子系统Windows POSIX subsystem

UNIX 程序员考虑的另一个选项是 Windows POSIX 子系统。Another option UNIX programmers look at is the Windows POSIX subsystem. 但是,它仅支持 POSIX 1003.1 版本,这是在创建 Windows NT 时唯一标准化的 POSIX 版本。However, it only supports POSIX 1003.1, which was the only POSIX version standardized when Windows NT was created. 自那时起,几乎不存在要扩展此子系统的需求,因为大多数应用程序已转换为 Win32。Since then, there has been little demand for extending this subsystem, because most applications have been converted to Win32. 1003.1 系统对功能完整的应用程序没有太大吸引力,因为它不具备的功能有很多(如 1003.2 中的功能、网络支持等)。The 1003.1 system is of limited interest for fully featured applications, because it does not include many capabilities (such as those in 1003.2, network support, and so on). Windows POSIX 子系统下运行的功能完整的应用程序无权访问可用于 Win32 应用程序的 Windows 功能,例如内存映射文件、联网和图形。Full featured applications run under the Windows POSIX subsystem do not have access to Windows features available to Win32 applications, such as memory-mapped files, networking, and graphics. VI、LS 和 GREP 等应用程序是 Windows POSIX 子系统的主要目标。Applications such as VI, LS, and GREP are the main targets for the Windows POSIX subsystem.

请参阅See Also

Visual C++ 移植和升级指南 Visual C++ Porting and Upgrading Guide
UNIX UNIX
推理规则Inference Rules