DLL_FAQ.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. Frequently Asked Questions about ZLIB1.DLL
  2. This document describes the design, the rationale, and the usage
  3. of the common DLL build of zlib, named ZLIB1.DLL. If you have
  4. general questions about zlib, you should see the file "FAQ" found
  5. in the zlib distribution, or at the following location:
  6. http://www.gzip.org/zlib/zlib_faq.html
  7. 1. What is ZLIB1.DLL, and how can I get it?
  8. - ZLIB1.DLL is the common build of zlib as a DLL.
  9. (Please remark the character '1' in the name.)
  10. Applications that link to ZLIB1.DLL can rely on the following
  11. specification:
  12. * The exported symbols are exclusively defined in the source
  13. files "zlib.h" and "zlib.def", found in an official zlib
  14. source distribution.
  15. * The symbols are exported by name, not by ordinal.
  16. * The exported names are undecorated.
  17. * The calling convention of functions is "C" (CDECL).
  18. * The ZLIB1.DLL binary is linked to MSVCRT.DLL.
  19. The archive in which ZLIB1.DLL is bundled contains compiled
  20. test programs that must run with a valid build of ZLIB1.DLL.
  21. It is recommended to download the prebuilt DLL from the zlib
  22. web site, instead of building it yourself, to avoid potential
  23. incompatibilities that could be introduced by your compiler
  24. and build settings. If you do build the DLL yourself, please
  25. make sure that it complies with all the above requirements,
  26. and it runs with the precompiled test programs, bundled with
  27. the original ZLIB1.DLL distribution.
  28. If, for any reason, you need to build an incompatible DLL,
  29. please use a different file name.
  30. 2. Why did you change the name of the DLL to ZLIB1.DLL?
  31. What happened to the old ZLIB.DLL?
  32. - The old ZLIB.DLL, built from zlib-1.1.4 or earlier, required
  33. compilation settings that were incompatible to those used by
  34. a static build. The DLL settings were supposed to be enabled
  35. by defining the macro ZLIB_DLL, before including "zlib.h".
  36. Incorrect handling of this macro was silently accepted at
  37. build time, resulting in two major problems:
  38. * ZLIB_DLL was missing from the old makefile. When building
  39. the DLL, not all people added it to the build options. In
  40. consequence, incompatible incarnations of ZLIB.DLL started
  41. to circulate around the net.
  42. * When switching from using the static library to using the
  43. DLL, applications had to define the ZLIB_DLL macro and
  44. to recompile all the sources that contained calls to zlib
  45. functions. Failure to do so resulted in creating binaries
  46. that were unable to run with the official ZLIB.DLL build.
  47. The only possible solution that we could foresee was to make
  48. a binary-incompatible change in the DLL interface, in order to
  49. remove the dependency on the ZLIB_DLL macro, and to release
  50. the new DLL under a different name.
  51. We chose the name ZLIB1.DLL, where '1' indicates the major
  52. zlib version number. We hope that we will not have to break
  53. the binary compatibility again, at least not as long as the
  54. zlib-1.x series will last.
  55. There is still a ZLIB_DLL macro, that can trigger a more
  56. efficient build and use of the DLL, but compatibility no
  57. longer dependents on it.
  58. 3. Can I build ZLIB.DLL from the new zlib sources, and replace
  59. an old ZLIB.DLL, that was built from zlib-1.1.4 or earlier?
  60. - In principle, you can do it by assigning calling convention
  61. keywords to the macros ZEXPORT and ZEXPORTVA. In practice,
  62. it depends on what you mean by "an old ZLIB.DLL", because the
  63. old DLL exists in several mutually-incompatible versions.
  64. You have to find out first what kind of calling convention is
  65. being used in your particular ZLIB.DLL build, and to use the
  66. same one in the new build. If you don't know what this is all
  67. about, you might be better off if you would just leave the old
  68. DLL intact.
  69. 4. Can I compile my application using the new zlib interface, and
  70. link it to an old ZLIB.DLL, that was built from zlib-1.1.4 or
  71. earlier?
  72. - The official answer is "no"; the real answer depends again on
  73. what kind of ZLIB.DLL you have. Even if you are lucky, this
  74. course of action is unreliable.
  75. If you rebuild your application and you intend to use a newer
  76. version of zlib (post- 1.1.4), it is strongly recommended to
  77. link it to the new ZLIB1.DLL.
  78. 5. Why are the zlib symbols exported by name, and not by ordinal?
  79. - Although exporting symbols by ordinal is a little faster, it
  80. is risky. Any single glitch in the maintenance or use of the
  81. DEF file that contains the ordinals can result in incompatible
  82. builds and frustrating crashes. Simply put, the benefits of
  83. exporting symbols by ordinal do not justify the risks.
  84. Technically, it should be possible to maintain ordinals in
  85. the DEF file, and still export the symbols by name. Ordinals
  86. exist in every DLL, and even if the dynamic linking performed
  87. at the DLL startup is searching for names, ordinals serve as
  88. hints, for a faster name lookup. However, if the DEF file
  89. contains ordinals, the Microsoft linker automatically builds
  90. an implib that will cause the executables linked to it to use
  91. those ordinals, and not the names. It is interesting to
  92. notice that the GNU linker for Win32 does not suffer from this
  93. problem.
  94. It is possible to avoid the DEF file if the exported symbols
  95. are accompanied by a "__declspec(dllexport)" attribute in the
  96. source files. You can do this in zlib by predefining the
  97. ZLIB_DLL macro.
  98. 6. I see that the ZLIB1.DLL functions use the "C" (CDECL) calling
  99. convention. Why not use the STDCALL convention?
  100. STDCALL is the standard convention in Win32, and I need it in
  101. my Visual Basic project!
  102. (For readability, we use CDECL to refer to the convention
  103. triggered by the "__cdecl" keyword, STDCALL to refer to
  104. the convention triggered by "__stdcall", and FASTCALL to
  105. refer to the convention triggered by "__fastcall".)
  106. - Most of the native Windows API functions (without varargs) use
  107. indeed the WINAPI convention (which translates to STDCALL in
  108. Win32), but the standard C functions use CDECL. If a user
  109. application is intrinsically tied to the Windows API (e.g.
  110. it calls native Windows API functions such as CreateFile()),
  111. sometimes it makes sense to decorate its own functions with
  112. WINAPI. But if ANSI C or POSIX portability is a goal (e.g.
  113. it calls standard C functions such as fopen()), it is not a
  114. sound decision to request the inclusion of <windows.h>, or to
  115. use non-ANSI constructs, for the sole purpose to make the user
  116. functions STDCALL-able.
  117. The functionality offered by zlib is not in the category of
  118. "Windows functionality", but is more like "C functionality".
  119. Technically, STDCALL is not bad; in fact, it is slightly
  120. faster than CDECL, and it works with variable-argument
  121. functions, just like CDECL. It is unfortunate that, in spite
  122. of using STDCALL in the Windows API, it is not the default
  123. convention used by the C compilers that run under Windows.
  124. The roots of the problem reside deep inside the unsafety of
  125. the K&R-style function prototypes, where the argument types
  126. are not specified; but that is another story for another day.
  127. The remaining fact is that CDECL is the default convention.
  128. Even if an explicit convention is hard-coded into the function
  129. prototypes inside C headers, problems may appear. The
  130. necessity to expose the convention in users' callbacks is one
  131. of these problems.
  132. The calling convention issues are also important when using
  133. zlib in other programming languages. Some of them, like Ada
  134. (GNAT) and Fortran (GNU G77), have C bindings implemented
  135. initially on Unix, and relying on the C calling convention.
  136. On the other hand, the pre- .NET versions of Microsoft Visual
  137. Basic require STDCALL, while Borland Delphi prefers, although
  138. it does not require, FASTCALL.
  139. In fairness to all possible uses of zlib outside the C
  140. programming language, we choose the default "C" convention.
  141. Anyone interested in different bindings or conventions is
  142. encouraged to maintain specialized projects. The "contrib/"
  143. directory from the zlib distribution already holds a couple
  144. of foreign bindings, such as Ada, C++, and Delphi.
  145. 7. I need a DLL for my Visual Basic project. What can I do?
  146. - Define the ZLIB_WINAPI macro before including "zlib.h", when
  147. building both the DLL and the user application (except that
  148. you don't need to define anything when using the DLL in Visual
  149. Basic). The ZLIB_WINAPI macro will switch on the WINAPI
  150. (STDCALL) convention. The name of this DLL must be different
  151. than the official ZLIB1.DLL.
  152. Gilles Vollant has contributed a build named ZLIBWAPI.DLL,
  153. with the ZLIB_WINAPI macro turned on, and with the minizip
  154. functionality built in. For more information, please read
  155. the notes inside "contrib/vstudio/readme.txt", found in the
  156. zlib distribution.
  157. 8. I need to use zlib in my Microsoft .NET project. What can I
  158. do?
  159. - Henrik Ravn has contributed a .NET wrapper around zlib. Look
  160. into contrib/dotzlib/, inside the zlib distribution.
  161. 9. If my application uses ZLIB1.DLL, should I link it to
  162. MSVCRT.DLL? Why?
  163. - It is not required, but it is recommended to link your
  164. application to MSVCRT.DLL, if it uses ZLIB1.DLL.
  165. The executables (.EXE, .DLL, etc.) that are involved in the
  166. same process and are using the C run-time library (i.e. they
  167. are calling standard C functions), must link to the same
  168. library. There are several libraries in the Win32 system:
  169. CRTDLL.DLL, MSVCRT.DLL, the static C libraries, etc.
  170. Since ZLIB1.DLL is linked to MSVCRT.DLL, the executables that
  171. depend on it should also be linked to MSVCRT.DLL.
  172. 10. Why are you saying that ZLIB1.DLL and my application should
  173. be linked to the same C run-time (CRT) library? I linked my
  174. application and my DLLs to different C libraries (e.g. my
  175. application to a static library, and my DLLs to MSVCRT.DLL),
  176. and everything works fine.
  177. - If a user library invokes only pure Win32 API (accessible via
  178. <windows.h> and the related headers), its DLL build will work
  179. in any context. But if this library invokes standard C API,
  180. things get more complicated.
  181. There is a single Win32 library in a Win32 system. Every
  182. function in this library resides in a single DLL module, that
  183. is safe to call from anywhere. On the other hand, there are
  184. multiple versions of the C library, and each of them has its
  185. own separate internal state. Standalone executables and user
  186. DLLs that call standard C functions must link to a C run-time
  187. (CRT) library, be it static or shared (DLL). Intermixing
  188. occurs when an executable (not necessarily standalone) and a
  189. DLL are linked to different CRTs, and both are running in the
  190. same process.
  191. Intermixing multiple CRTs is possible, as long as their
  192. internal states are kept intact. The Microsoft Knowledge Base
  193. articles KB94248 "HOWTO: Use the C Run-Time" and KB140584
  194. "HOWTO: Link with the Correct C Run-Time (CRT) Library"
  195. mention the potential problems raised by intermixing.
  196. If intermixing works for you, it's because your application
  197. and DLLs are avoiding the corruption of each of the CRTs'
  198. internal states, maybe by careful design, or maybe by fortune.
  199. Also note that linking ZLIB1.DLL to non-Microsoft CRTs, such
  200. as those provided by Borland, raises similar problems.
  201. 11. Why are you linking ZLIB1.DLL to MSVCRT.DLL?
  202. - MSVCRT.DLL exists on every Windows 95 with a new service pack
  203. installed, or with Microsoft Internet Explorer 4 or later, and
  204. on all other Windows 4.x or later (Windows 98, Windows NT 4,
  205. or later). It is freely distributable; if not present in the
  206. system, it can be downloaded from Microsoft or from other
  207. software provider for free.
  208. The fact that MSVCRT.DLL does not exist on a virgin Windows 95
  209. is not so problematic. Windows 95 is scarcely found nowadays,
  210. Microsoft ended its support a long time ago, and many recent
  211. applications from various vendors, including Microsoft, do not
  212. even run on it. Furthermore, no serious user should run
  213. Windows 95 without a proper update installed.
  214. 12. Why are you not linking ZLIB1.DLL to
  215. <<my favorite C run-time library>> ?
  216. - We considered and abandoned the following alternatives:
  217. * Linking ZLIB1.DLL to a static C library (LIBC.LIB, or
  218. LIBCMT.LIB) is not a good option. People are using the DLL
  219. mainly to save disk space. If you are linking your program
  220. to a static C library, you may as well consider linking zlib
  221. in statically, too.
  222. * Linking ZLIB1.DLL to CRTDLL.DLL looks appealing, because
  223. CRTDLL.DLL is present on every Win32 installation.
  224. Unfortunately, it has a series of problems: it does not
  225. work properly with Microsoft's C++ libraries, it does not
  226. provide support for 64-bit file offsets, (and so on...),
  227. and Microsoft discontinued its support a long time ago.
  228. * Linking ZLIB1.DLL to MSVCR70.DLL or MSVCR71.DLL, supplied
  229. with the Microsoft .NET platform, and Visual C++ 7.0/7.1,
  230. raises problems related to the status of ZLIB1.DLL as a
  231. system component. According to the Microsoft Knowledge Base
  232. article KB326922 "INFO: Redistribution of the Shared C
  233. Runtime Component in Visual C++ .NET", MSVCR70.DLL and
  234. MSVCR71.DLL are not supposed to function as system DLLs,
  235. because they may clash with MSVCRT.DLL. Instead, the
  236. application's installer is supposed to put these DLLs
  237. (if needed) in the application's private directory.
  238. If ZLIB1.DLL depends on a non-system runtime, it cannot
  239. function as a redistributable system component.
  240. * Linking ZLIB1.DLL to non-Microsoft runtimes, such as
  241. Borland's, or Cygwin's, raises problems related to the
  242. reliable presence of these runtimes on Win32 systems.
  243. It's easier to let the DLL build of zlib up to the people
  244. who distribute these runtimes, and who may proceed as
  245. explained in the answer to Question 14.
  246. 13. If ZLIB1.DLL cannot be linked to MSVCR70.DLL or MSVCR71.DLL,
  247. how can I build/use ZLIB1.DLL in Microsoft Visual C++ 7.0
  248. (Visual Studio .NET) or newer?
  249. - Due to the problems explained in the Microsoft Knowledge Base
  250. article KB326922 (see the previous answer), the C runtime that
  251. comes with the VC7 environment is no longer considered a
  252. system component. That is, it should not be assumed that this
  253. runtime exists, or may be installed in a system directory.
  254. Since ZLIB1.DLL is supposed to be a system component, it may
  255. not depend on a non-system component.
  256. In order to link ZLIB1.DLL and your application to MSVCRT.DLL
  257. in VC7, you need the library of Visual C++ 6.0 or older. If
  258. you don't have this library at hand, it's probably best not to
  259. use ZLIB1.DLL.
  260. We are hoping that, in the future, Microsoft will provide a
  261. way to build applications linked to a proper system runtime,
  262. from the Visual C++ environment. Until then, you have a
  263. couple of alternatives, such as linking zlib in statically.
  264. If your application requires dynamic linking, you may proceed
  265. as explained in the answer to Question 14.
  266. 14. I need to link my own DLL build to a CRT different than
  267. MSVCRT.DLL. What can I do?
  268. - Feel free to rebuild the DLL from the zlib sources, and link
  269. it the way you want. You should, however, clearly state that
  270. your build is unofficial. You should give it a different file
  271. name, and/or install it in a private directory that can be
  272. accessed by your application only, and is not visible to the
  273. others (i.e. it's neither in the PATH, nor in the SYSTEM or
  274. SYSTEM32 directories). Otherwise, your build may clash with
  275. applications that link to the official build.
  276. For example, in Cygwin, zlib is linked to the Cygwin runtime
  277. CYGWIN1.DLL, and it is distributed under the name CYGZ.DLL.
  278. 15. May I include additional pieces of code that I find useful,
  279. link them in ZLIB1.DLL, and export them?
  280. - No. A legitimate build of ZLIB1.DLL must not include code
  281. that does not originate from the official zlib source code.
  282. But you can make your own private DLL build, under a different
  283. file name, as suggested in the previous answer.
  284. For example, zlib is a part of the VCL library, distributed
  285. with Borland Delphi and C++ Builder. The DLL build of VCL
  286. is a redistributable file, named VCLxx.DLL.
  287. 16. May I remove some functionality out of ZLIB1.DLL, by enabling
  288. macros like NO_GZCOMPRESS or NO_GZIP at compile time?
  289. - No. A legitimate build of ZLIB1.DLL must provide the complete
  290. zlib functionality, as implemented in the official zlib source
  291. code. But you can make your own private DLL build, under a
  292. different file name, as suggested in the previous answer.
  293. **
  294. This document is written and maintained by
  295. Cosmin Truta <cosmint@cs.ubbcluj.ro>