Category: Programming  ·  Originally posted August 7, 2013  ·  Last updated August 7, 2013

How to compile zLib 1.2.8 (WINAPI / WAPI / STDCALL version)

This is not a full-blown article – just a quick HOWTO for people who require the latest version of zLib and want to use it from a language (like classic VB) that requires a standard WinAPI interface.

By default, zLib uses C calling conventions (CDECL). Gilles Vollant has helpfully provided an STDCALL version of zLib in the past, but his site only provides version 1.2.5, which dates back to January 2012. zLib is currently on version 1.2.8.

Gilles has contributed his WAPI fixes to the core zLib distribution so that anyone can compile it themselves. To do this, you will need to download:

Once both of those are downloaded (and updated, as VS 2012 will require you to install several service packs), follow these steps to compile zLib yourself:

  1. Extract the entire zLib file and navigate to the /contrib/masmx86 folder. Open the “bld_ml32.bat” file in a text editor.
  2. Add the “/safeseh” switch to both lines in that file (e.g. “ml /safeseh /coff /Zi /c /Flmatch686.lst match686.asm”). Then save and exit.
  3. Navigate to the /contrib/vstudio/vc11/ folder. Open the zlibvc.sln file in your newly installed Visual Studio 2012 Express.
  4. In the Solution Explorer (top-right by default), right-click “zlibstat” then select “Properties” at the bottom.
  5. Go to Configuration Properties -> C/C++ -> Preprocessor, and in the Preprocessor Definitions line remove “ZLIB_WINAPI;” (don’t forget to remove the trailing semicolon).
  6. Now, we need to fix a recently introduced problem that relies on Win8 functionality. In the Solution Explorer, navigate to zlibvc -> iowin32.c. Double-click to open the file.
  7. Find the line of text that reads “#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)”. Change this line to “#if WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY_DESKTOP_APP, WINAPI_PARTITION_APP)”. (Thanks to this link for this fix.)
  8. zLib uses a Version number declaration that can cause the build process to fail. To fix this, go back to the Solution Explorer, then navigate to zlibvc -> zlibvc.def. Double-click to open.
  9. Change the line that reads “VERSION 1.2.8″ to read “VERSION 1.28″.
  10. Finally, go to the Build -> Configuration Manager menu and change the Active Solution Configuration to “Release”.
  11. Exit that window and press F7 (or click the Build -> Build Solution menu). The project should successfully build.
  12. You can find your newly compiled zlibwapi.dll file in the /contrib/vstudio/vc11/x86/ZlibDllRelease/ folder.

bookmark and share this article

Email this article to a friend Print a copy of this article Subscribe to article comments Bookmark this article on Delicious Submit this article to reddit Submit this article to Digg Submit this article to StumbleUpon Share this article on Facebook Tweet this article

related articles

Discussion (9 comments)

  1. Thanks for saving my time :)

    By Mohsen | April 3, 2014, 6:21 am | Reply to this comment
  2. OMG, I spent 2 days googling and trying to fix this issue.

    THANK YOU, THANK YOU, THANK YOU!!!

    By Dan | November 24, 2014, 6:20 pm | Reply to this comment
  3. Thanks a lot! I was struggling to get this working, and your article helped a ton!

    By Randy D | December 24, 2014, 8:49 am | Reply to this comment
  4. Removing this preprocessor define ZLIB_WINAPI will result in using cdecl calling convention, instead of stdcall.
    So in fact using it in VS you have to define this macro.

    You can read about this in the FAQ: http://www.zlib.net/DLL_FAQ.txt Point 7.

    By zack | January 28, 2015, 3:37 am | Reply to this comment
    • ZLIB_WINAPI is still defined in the zlibvc project.

      By Tanner | January 28, 2015, 7:01 am | Reply to this comment
      • Yeah I know, but why did you write in your description to remove this define? This doesn’t make sense, when you want to use stdcall convention.

        By zack | January 28, 2015, 8:25 am |
      • Because the definition in zlibstat is bugged, and it causes the program to not compile under the latest Visual Studio releases. (See http://stackoverflow.com/questions/5424549/unresolved-externals-despite-linking-in-zlib-lib, for example.)

        The separate definition in zlibvc is sufficient to enable stdcall support. This is easily tested on the output of the DLL generated by the steps above.

        By Tanner | January 28, 2015, 9:32 am |
  5. I use VS 2013 Update 4, and I have this bug in zlibvc using win32 toolchain. With x64 everyhing works fine.

    1)
    So I think I will remove this ZLIB_WINAPI #define in every target for every architecture type. Do I still have to call this library then zlibwapi.dll?

    2)
    I only use zlib in C++ code, how do you setup you lib?

    3)
    I used only step 1) and 9). Do I really need to do step 7) when I don’t get a compile error?

    By zack | January 28, 2015, 9:56 am | Reply to this comment
    • I think you might get better help with these questions on a site like StackOverflow. They have a number of solutions dealing with using zLib as a static library instead of a dynamic one, which I assume is what you’re trying to do (from your point #2). Good luck.

      By Tanner | January 30, 2015, 6:42 am | Reply to this comment

Share your thoughts below

Comments support the following tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>