打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
winapi

(This question uses zlib as an example but isn't specific to it.)

I'm trying to compile zlib on Windows using the MSVC project file it comes with. I'm using VS2012 with the Windows 8 SDK, but my build machine is Windows 7.

Zlib contains the following code:

#ifdef IOWIN32_USING_WINRT_API    if ((filename!=NULL) && (dwDesiredAccess != 0))        hFile = CreateFile2((LPCTSTR)filename, dwDesiredAccess, dwShareMode, dwCreationDisposition, NULL);#else    if ((filename!=NULL) && (dwDesiredAccess != 0))        hFile = CreateFile((LPCTSTR)filename, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, dwFlagsAndAttributes, NULL);#endif

where IOWIN32_USING_WINRT_API is set up as follows

#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)#define IOWIN32_USING_WINRT_API 1#endif#endif

WINAPI_FAMILY_PARTITION and WINAPI_PARTITION_APP are defined in the Windows 8 SDK (winapifamily.h), so the result is that the code that uses CreateFile2 in zlib is available to be compiled.

I can compile zlib, but any application that tries to load it fails because it can't find CreateFile2, and rightly so, since my machine is Windows 7 and that API was introduced in Windows 8.

This is the first time I've ever used a Windows SDK on a lower version of Windows than it supports, hence my question:

Is my only recourse to patch zlib to add a check for _WIN32_WINNT >= _WIN32_WINNT_WIN8 to the very first line, and pass /D_WIN32_WINNT=_WIN32_WINNT_WIN7 to msbuild? That would mean I'd have to build two sets of binaries - one that supports Win7 (by passing the /D) and one that supports Win8 (by not passing the /D).

Is there no way to have a common binary that works on both Windows 7 and 8 and uses the CreateFile2 codepath if running on Windows 8? (I suppose that way is GetProcAddress + function pointers. Anything else?)

asked Jun 14 '13 at 6:56
Arnavion
175211

    
Actually, the Win7 library will work fine on Win8 for desktop apps. CreateFile is only disable for WinRT aka "Windows Store" apps. –  Igor Skochinsky Jun 14 '13 at 11:36
    
So just to confirm, was I mistaken to believe that the Win8 SDK can be used to compile programs unchanged for both Win7 and Win8? If I want to target Windows version X, am I always supposed to use the SDK version X rather than a later SDK? –  Arnavion Jun 14 '13 at 15:54
    
No, see my answer. What I meant is you can use /D_WIN32_WINNT=_WIN32_WINNT_WIN7 to build a Win7 library and then use it in both OS. –  Igor Skochinsky Jun 14 '13 at 16:27

4 Answers

You can use _WIN32_WINNT to select which OS version you want to target. It's the zlib check which is broken. WINAPI_FAMILY_DESKTOP_APP includes WINAPI_FAMILY_APP so their check always succeeds. You can fix it like:

#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))#if WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY_DESKTOP_APP, WINAPI_PARTITION_APP)#define IOWIN32_USING_WINRT_API 1#endif#endif

See here for more info: http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/3965645c-978c-4148-b32c-1853f7fd22b3

answered Jun 14 '13 at 16:26
Igor Skochinsky
14.7k12250

    
Thanks for confirming that the /D is required. –  Arnavion Jun 15 '13 at 18:42
    
Sorry, this is wrong. I think you also need to add a condition to the first #if in your answer to check for _WIN32_WINNT >= _WIN32_WINNT_WIN8. Please confirm. –  Arnavion Jun 15 '13 at 19:47

You can do this is you have at least the Update 1 installed for VS2012 because this gives you the ability to compile for XP, which links against a cut down version of the Windows 7 SDK.

We do this with some of our code and it is compatible across XP, Windows 7 and Windows 8.

See the MSDN blog here for details.

Crucial information below

answered Jun 14 '13 at 10:33
Roger Rowland
15.4k82552

This question is old but if anyone still has this problem, I just succeded building latest zlib with msvc 12.0 targeting windows 7 by modifying the code in (iowin32.c file line 28) like so:

#if _WIN32_WINNT >= _WIN32_WINNT_WIN8#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)#define IOWIN32_USING_WINRT_API 1#endif#endif#endif

of course to make this work you need to define a macro in:configuration properties >C/C++ > preprocessor:

_WIN32_WINNT=0x0601

which means you're targeting windows 7 (if using windows 7 SDK this is not needed)

then rebuilding the project with success :)and yes, of course this fix is only aplicable if you're using a windows olderthan windows 8.

however this is not the only error that zlib has, you will for sure encounter a mismatched version in zlib.def file.to fix this open the zlib.def file that contains the error and replace 1.2.8 with 1.28or whatever the version is

answered Aug 12 '14 at 7:39
codekiddy
1,55711439

    
That is correct. It's the same thing we do at the gtk-win32 project: github.com/hexchat/gtk-win32/tree/master/zlib/contrib/vstudio/… –  Arnavion Aug 14 '14 at 7:15

As far as I know, the only way is to use LoadLibrary() and GetProcAddress() -- you can also see this in MFC Sources which handle messages native to WIn7 (or later)

e.g.: CWnd::OnGesture() in %VCINSTALLDIR%atlmfc\src\mfc\wincore.cpp

answered Jun 14 '13 at 8:11
Edward Clements
3,6622515

Your Answer
















Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Stack Exchange

Post as a guest

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Windows下zlib库和libPng库的编译和使用
VS2010编译错误:fatal error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x
教你如何在DOS下安装xp系统
系統不能进入桌面如何还原系统
电脑故障以及解决 百科全书 (菜鸟荤鸟,是鸟必备)
解读boot.ini
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服