cannam@128: cannam@128: Frequently Asked Questions about ZLIB1.DLL cannam@128: cannam@128: cannam@128: This document describes the design, the rationale, and the usage cannam@128: of the official DLL build of zlib, named ZLIB1.DLL. If you have cannam@128: general questions about zlib, you should see the file "FAQ" found cannam@128: in the zlib distribution, or at the following location: cannam@128: http://www.gzip.org/zlib/zlib_faq.html cannam@128: cannam@128: cannam@128: 1. What is ZLIB1.DLL, and how can I get it? cannam@128: cannam@128: - ZLIB1.DLL is the official build of zlib as a DLL. cannam@128: (Please remark the character '1' in the name.) cannam@128: cannam@128: Pointers to a precompiled ZLIB1.DLL can be found in the zlib cannam@128: web site at: cannam@128: http://www.zlib.net/ cannam@128: cannam@128: Applications that link to ZLIB1.DLL can rely on the following cannam@128: specification: cannam@128: cannam@128: * The exported symbols are exclusively defined in the source cannam@128: files "zlib.h" and "zlib.def", found in an official zlib cannam@128: source distribution. cannam@128: * The symbols are exported by name, not by ordinal. cannam@128: * The exported names are undecorated. cannam@128: * The calling convention of functions is "C" (CDECL). cannam@128: * The ZLIB1.DLL binary is linked to MSVCRT.DLL. cannam@128: cannam@128: The archive in which ZLIB1.DLL is bundled contains compiled cannam@128: test programs that must run with a valid build of ZLIB1.DLL. cannam@128: It is recommended to download the prebuilt DLL from the zlib cannam@128: web site, instead of building it yourself, to avoid potential cannam@128: incompatibilities that could be introduced by your compiler cannam@128: and build settings. If you do build the DLL yourself, please cannam@128: make sure that it complies with all the above requirements, cannam@128: and it runs with the precompiled test programs, bundled with cannam@128: the original ZLIB1.DLL distribution. cannam@128: cannam@128: If, for any reason, you need to build an incompatible DLL, cannam@128: please use a different file name. cannam@128: cannam@128: cannam@128: 2. Why did you change the name of the DLL to ZLIB1.DLL? cannam@128: What happened to the old ZLIB.DLL? cannam@128: cannam@128: - The old ZLIB.DLL, built from zlib-1.1.4 or earlier, required cannam@128: compilation settings that were incompatible to those used by cannam@128: a static build. The DLL settings were supposed to be enabled cannam@128: by defining the macro ZLIB_DLL, before including "zlib.h". cannam@128: Incorrect handling of this macro was silently accepted at cannam@128: build time, resulting in two major problems: cannam@128: cannam@128: * ZLIB_DLL was missing from the old makefile. When building cannam@128: the DLL, not all people added it to the build options. In cannam@128: consequence, incompatible incarnations of ZLIB.DLL started cannam@128: to circulate around the net. cannam@128: cannam@128: * When switching from using the static library to using the cannam@128: DLL, applications had to define the ZLIB_DLL macro and cannam@128: to recompile all the sources that contained calls to zlib cannam@128: functions. Failure to do so resulted in creating binaries cannam@128: that were unable to run with the official ZLIB.DLL build. cannam@128: cannam@128: The only possible solution that we could foresee was to make cannam@128: a binary-incompatible change in the DLL interface, in order to cannam@128: remove the dependency on the ZLIB_DLL macro, and to release cannam@128: the new DLL under a different name. cannam@128: cannam@128: We chose the name ZLIB1.DLL, where '1' indicates the major cannam@128: zlib version number. We hope that we will not have to break cannam@128: the binary compatibility again, at least not as long as the cannam@128: zlib-1.x series will last. cannam@128: cannam@128: There is still a ZLIB_DLL macro, that can trigger a more cannam@128: efficient build and use of the DLL, but compatibility no cannam@128: longer dependents on it. cannam@128: cannam@128: cannam@128: 3. Can I build ZLIB.DLL from the new zlib sources, and replace cannam@128: an old ZLIB.DLL, that was built from zlib-1.1.4 or earlier? cannam@128: cannam@128: - In principle, you can do it by assigning calling convention cannam@128: keywords to the macros ZEXPORT and ZEXPORTVA. In practice, cannam@128: it depends on what you mean by "an old ZLIB.DLL", because the cannam@128: old DLL exists in several mutually-incompatible versions. cannam@128: You have to find out first what kind of calling convention is cannam@128: being used in your particular ZLIB.DLL build, and to use the cannam@128: same one in the new build. If you don't know what this is all cannam@128: about, you might be better off if you would just leave the old cannam@128: DLL intact. cannam@128: cannam@128: cannam@128: 4. Can I compile my application using the new zlib interface, and cannam@128: link it to an old ZLIB.DLL, that was built from zlib-1.1.4 or cannam@128: earlier? cannam@128: cannam@128: - The official answer is "no"; the real answer depends again on cannam@128: what kind of ZLIB.DLL you have. Even if you are lucky, this cannam@128: course of action is unreliable. cannam@128: cannam@128: If you rebuild your application and you intend to use a newer cannam@128: version of zlib (post- 1.1.4), it is strongly recommended to cannam@128: link it to the new ZLIB1.DLL. cannam@128: cannam@128: cannam@128: 5. Why are the zlib symbols exported by name, and not by ordinal? cannam@128: cannam@128: - Although exporting symbols by ordinal is a little faster, it cannam@128: is risky. Any single glitch in the maintenance or use of the cannam@128: DEF file that contains the ordinals can result in incompatible cannam@128: builds and frustrating crashes. Simply put, the benefits of cannam@128: exporting symbols by ordinal do not justify the risks. cannam@128: cannam@128: Technically, it should be possible to maintain ordinals in cannam@128: the DEF file, and still export the symbols by name. Ordinals cannam@128: exist in every DLL, and even if the dynamic linking performed cannam@128: at the DLL startup is searching for names, ordinals serve as cannam@128: hints, for a faster name lookup. However, if the DEF file cannam@128: contains ordinals, the Microsoft linker automatically builds cannam@128: an implib that will cause the executables linked to it to use cannam@128: those ordinals, and not the names. It is interesting to cannam@128: notice that the GNU linker for Win32 does not suffer from this cannam@128: problem. cannam@128: cannam@128: It is possible to avoid the DEF file if the exported symbols cannam@128: are accompanied by a "__declspec(dllexport)" attribute in the cannam@128: source files. You can do this in zlib by predefining the cannam@128: ZLIB_DLL macro. cannam@128: cannam@128: cannam@128: 6. I see that the ZLIB1.DLL functions use the "C" (CDECL) calling cannam@128: convention. Why not use the STDCALL convention? cannam@128: STDCALL is the standard convention in Win32, and I need it in cannam@128: my Visual Basic project! cannam@128: cannam@128: (For readability, we use CDECL to refer to the convention cannam@128: triggered by the "__cdecl" keyword, STDCALL to refer to cannam@128: the convention triggered by "__stdcall", and FASTCALL to cannam@128: refer to the convention triggered by "__fastcall".) cannam@128: cannam@128: - Most of the native Windows API functions (without varargs) use cannam@128: indeed the WINAPI convention (which translates to STDCALL in cannam@128: Win32), but the standard C functions use CDECL. If a user cannam@128: application is intrinsically tied to the Windows API (e.g. cannam@128: it calls native Windows API functions such as CreateFile()), cannam@128: sometimes it makes sense to decorate its own functions with cannam@128: WINAPI. But if ANSI C or POSIX portability is a goal (e.g. cannam@128: it calls standard C functions such as fopen()), it is not a cannam@128: sound decision to request the inclusion of , or to cannam@128: use non-ANSI constructs, for the sole purpose to make the user cannam@128: functions STDCALL-able. cannam@128: cannam@128: The functionality offered by zlib is not in the category of cannam@128: "Windows functionality", but is more like "C functionality". cannam@128: cannam@128: Technically, STDCALL is not bad; in fact, it is slightly cannam@128: faster than CDECL, and it works with variable-argument cannam@128: functions, just like CDECL. It is unfortunate that, in spite cannam@128: of using STDCALL in the Windows API, it is not the default cannam@128: convention used by the C compilers that run under Windows. cannam@128: The roots of the problem reside deep inside the unsafety of cannam@128: the K&R-style function prototypes, where the argument types cannam@128: are not specified; but that is another story for another day. cannam@128: cannam@128: The remaining fact is that CDECL is the default convention. cannam@128: Even if an explicit convention is hard-coded into the function cannam@128: prototypes inside C headers, problems may appear. The cannam@128: necessity to expose the convention in users' callbacks is one cannam@128: of these problems. cannam@128: cannam@128: The calling convention issues are also important when using cannam@128: zlib in other programming languages. Some of them, like Ada cannam@128: (GNAT) and Fortran (GNU G77), have C bindings implemented cannam@128: initially on Unix, and relying on the C calling convention. cannam@128: On the other hand, the pre- .NET versions of Microsoft Visual cannam@128: Basic require STDCALL, while Borland Delphi prefers, although cannam@128: it does not require, FASTCALL. cannam@128: cannam@128: In fairness to all possible uses of zlib outside the C cannam@128: programming language, we choose the default "C" convention. cannam@128: Anyone interested in different bindings or conventions is cannam@128: encouraged to maintain specialized projects. The "contrib/" cannam@128: directory from the zlib distribution already holds a couple cannam@128: of foreign bindings, such as Ada, C++, and Delphi. cannam@128: cannam@128: cannam@128: 7. I need a DLL for my Visual Basic project. What can I do? cannam@128: cannam@128: - Define the ZLIB_WINAPI macro before including "zlib.h", when cannam@128: building both the DLL and the user application (except that cannam@128: you don't need to define anything when using the DLL in Visual cannam@128: Basic). The ZLIB_WINAPI macro will switch on the WINAPI cannam@128: (STDCALL) convention. The name of this DLL must be different cannam@128: than the official ZLIB1.DLL. cannam@128: cannam@128: Gilles Vollant has contributed a build named ZLIBWAPI.DLL, cannam@128: with the ZLIB_WINAPI macro turned on, and with the minizip cannam@128: functionality built in. For more information, please read cannam@128: the notes inside "contrib/vstudio/readme.txt", found in the cannam@128: zlib distribution. cannam@128: cannam@128: cannam@128: 8. I need to use zlib in my Microsoft .NET project. What can I cannam@128: do? cannam@128: cannam@128: - Henrik Ravn has contributed a .NET wrapper around zlib. Look cannam@128: into contrib/dotzlib/, inside the zlib distribution. cannam@128: cannam@128: cannam@128: 9. If my application uses ZLIB1.DLL, should I link it to cannam@128: MSVCRT.DLL? Why? cannam@128: cannam@128: - It is not required, but it is recommended to link your cannam@128: application to MSVCRT.DLL, if it uses ZLIB1.DLL. cannam@128: cannam@128: The executables (.EXE, .DLL, etc.) that are involved in the cannam@128: same process and are using the C run-time library (i.e. they cannam@128: are calling standard C functions), must link to the same cannam@128: library. There are several libraries in the Win32 system: cannam@128: CRTDLL.DLL, MSVCRT.DLL, the static C libraries, etc. cannam@128: Since ZLIB1.DLL is linked to MSVCRT.DLL, the executables that cannam@128: depend on it should also be linked to MSVCRT.DLL. cannam@128: cannam@128: cannam@128: 10. Why are you saying that ZLIB1.DLL and my application should cannam@128: be linked to the same C run-time (CRT) library? I linked my cannam@128: application and my DLLs to different C libraries (e.g. my cannam@128: application to a static library, and my DLLs to MSVCRT.DLL), cannam@128: and everything works fine. cannam@128: cannam@128: - If a user library invokes only pure Win32 API (accessible via cannam@128: and the related headers), its DLL build will work cannam@128: in any context. But if this library invokes standard C API, cannam@128: things get more complicated. cannam@128: cannam@128: There is a single Win32 library in a Win32 system. Every cannam@128: function in this library resides in a single DLL module, that cannam@128: is safe to call from anywhere. On the other hand, there are cannam@128: multiple versions of the C library, and each of them has its cannam@128: own separate internal state. Standalone executables and user cannam@128: DLLs that call standard C functions must link to a C run-time cannam@128: (CRT) library, be it static or shared (DLL). Intermixing cannam@128: occurs when an executable (not necessarily standalone) and a cannam@128: DLL are linked to different CRTs, and both are running in the cannam@128: same process. cannam@128: cannam@128: Intermixing multiple CRTs is possible, as long as their cannam@128: internal states are kept intact. The Microsoft Knowledge Base cannam@128: articles KB94248 "HOWTO: Use the C Run-Time" and KB140584 cannam@128: "HOWTO: Link with the Correct C Run-Time (CRT) Library" cannam@128: mention the potential problems raised by intermixing. cannam@128: cannam@128: If intermixing works for you, it's because your application cannam@128: and DLLs are avoiding the corruption of each of the CRTs' cannam@128: internal states, maybe by careful design, or maybe by fortune. cannam@128: cannam@128: Also note that linking ZLIB1.DLL to non-Microsoft CRTs, such cannam@128: as those provided by Borland, raises similar problems. cannam@128: cannam@128: cannam@128: 11. Why are you linking ZLIB1.DLL to MSVCRT.DLL? cannam@128: cannam@128: - MSVCRT.DLL exists on every Windows 95 with a new service pack cannam@128: installed, or with Microsoft Internet Explorer 4 or later, and cannam@128: on all other Windows 4.x or later (Windows 98, Windows NT 4, cannam@128: or later). It is freely distributable; if not present in the cannam@128: system, it can be downloaded from Microsoft or from other cannam@128: software provider for free. cannam@128: cannam@128: The fact that MSVCRT.DLL does not exist on a virgin Windows 95 cannam@128: is not so problematic. Windows 95 is scarcely found nowadays, cannam@128: Microsoft ended its support a long time ago, and many recent cannam@128: applications from various vendors, including Microsoft, do not cannam@128: even run on it. Furthermore, no serious user should run cannam@128: Windows 95 without a proper update installed. cannam@128: cannam@128: cannam@128: 12. Why are you not linking ZLIB1.DLL to cannam@128: <> ? cannam@128: cannam@128: - We considered and abandoned the following alternatives: cannam@128: cannam@128: * Linking ZLIB1.DLL to a static C library (LIBC.LIB, or cannam@128: LIBCMT.LIB) is not a good option. People are using the DLL cannam@128: mainly to save disk space. If you are linking your program cannam@128: to a static C library, you may as well consider linking zlib cannam@128: in statically, too. cannam@128: cannam@128: * Linking ZLIB1.DLL to CRTDLL.DLL looks appealing, because cannam@128: CRTDLL.DLL is present on every Win32 installation. cannam@128: Unfortunately, it has a series of problems: it does not cannam@128: work properly with Microsoft's C++ libraries, it does not cannam@128: provide support for 64-bit file offsets, (and so on...), cannam@128: and Microsoft discontinued its support a long time ago. cannam@128: cannam@128: * Linking ZLIB1.DLL to MSVCR70.DLL or MSVCR71.DLL, supplied cannam@128: with the Microsoft .NET platform, and Visual C++ 7.0/7.1, cannam@128: raises problems related to the status of ZLIB1.DLL as a cannam@128: system component. According to the Microsoft Knowledge Base cannam@128: article KB326922 "INFO: Redistribution of the Shared C cannam@128: Runtime Component in Visual C++ .NET", MSVCR70.DLL and cannam@128: MSVCR71.DLL are not supposed to function as system DLLs, cannam@128: because they may clash with MSVCRT.DLL. Instead, the cannam@128: application's installer is supposed to put these DLLs cannam@128: (if needed) in the application's private directory. cannam@128: If ZLIB1.DLL depends on a non-system runtime, it cannot cannam@128: function as a redistributable system component. cannam@128: cannam@128: * Linking ZLIB1.DLL to non-Microsoft runtimes, such as cannam@128: Borland's, or Cygwin's, raises problems related to the cannam@128: reliable presence of these runtimes on Win32 systems. cannam@128: It's easier to let the DLL build of zlib up to the people cannam@128: who distribute these runtimes, and who may proceed as cannam@128: explained in the answer to Question 14. cannam@128: cannam@128: cannam@128: 13. If ZLIB1.DLL cannot be linked to MSVCR70.DLL or MSVCR71.DLL, cannam@128: how can I build/use ZLIB1.DLL in Microsoft Visual C++ 7.0 cannam@128: (Visual Studio .NET) or newer? cannam@128: cannam@128: - Due to the problems explained in the Microsoft Knowledge Base cannam@128: article KB326922 (see the previous answer), the C runtime that cannam@128: comes with the VC7 environment is no longer considered a cannam@128: system component. That is, it should not be assumed that this cannam@128: runtime exists, or may be installed in a system directory. cannam@128: Since ZLIB1.DLL is supposed to be a system component, it may cannam@128: not depend on a non-system component. cannam@128: cannam@128: In order to link ZLIB1.DLL and your application to MSVCRT.DLL cannam@128: in VC7, you need the library of Visual C++ 6.0 or older. If cannam@128: you don't have this library at hand, it's probably best not to cannam@128: use ZLIB1.DLL. cannam@128: cannam@128: We are hoping that, in the future, Microsoft will provide a cannam@128: way to build applications linked to a proper system runtime, cannam@128: from the Visual C++ environment. Until then, you have a cannam@128: couple of alternatives, such as linking zlib in statically. cannam@128: If your application requires dynamic linking, you may proceed cannam@128: as explained in the answer to Question 14. cannam@128: cannam@128: cannam@128: 14. I need to link my own DLL build to a CRT different than cannam@128: MSVCRT.DLL. What can I do? cannam@128: cannam@128: - Feel free to rebuild the DLL from the zlib sources, and link cannam@128: it the way you want. You should, however, clearly state that cannam@128: your build is unofficial. You should give it a different file cannam@128: name, and/or install it in a private directory that can be cannam@128: accessed by your application only, and is not visible to the cannam@128: others (i.e. it's neither in the PATH, nor in the SYSTEM or cannam@128: SYSTEM32 directories). Otherwise, your build may clash with cannam@128: applications that link to the official build. cannam@128: cannam@128: For example, in Cygwin, zlib is linked to the Cygwin runtime cannam@128: CYGWIN1.DLL, and it is distributed under the name CYGZ.DLL. cannam@128: cannam@128: cannam@128: 15. May I include additional pieces of code that I find useful, cannam@128: link them in ZLIB1.DLL, and export them? cannam@128: cannam@128: - No. A legitimate build of ZLIB1.DLL must not include code cannam@128: that does not originate from the official zlib source code. cannam@128: But you can make your own private DLL build, under a different cannam@128: file name, as suggested in the previous answer. cannam@128: cannam@128: For example, zlib is a part of the VCL library, distributed cannam@128: with Borland Delphi and C++ Builder. The DLL build of VCL cannam@128: is a redistributable file, named VCLxx.DLL. cannam@128: cannam@128: cannam@128: 16. May I remove some functionality out of ZLIB1.DLL, by enabling cannam@128: macros like NO_GZCOMPRESS or NO_GZIP at compile time? cannam@128: cannam@128: - No. A legitimate build of ZLIB1.DLL must provide the complete cannam@128: zlib functionality, as implemented in the official zlib source cannam@128: code. But you can make your own private DLL build, under a cannam@128: different file name, as suggested in the previous answer. cannam@128: cannam@128: cannam@128: 17. I made my own ZLIB1.DLL build. Can I test it for compliance? cannam@128: cannam@128: - We prefer that you download the official DLL from the zlib cannam@128: web site. If you need something peculiar from this DLL, you cannam@128: can send your suggestion to the zlib mailing list. cannam@128: cannam@128: However, in case you do rebuild the DLL yourself, you can run cannam@128: it with the test programs found in the DLL distribution. cannam@128: Running these test programs is not a guarantee of compliance, cannam@128: but a failure can imply a detected problem. cannam@128: cannam@128: ** cannam@128: cannam@128: This document is written and maintained by cannam@128: Cosmin Truta