Search code examples
c++windowswinapiwindows-xp

Build my application for a previous Windows platform


Background

I'm writing a C++ app using VS2015 on WIN 7. This app will run on all windows OS greater than equal to XP.

Through out my code, I use lots of WINAPI calls.

I wish to prevent, at compile time, the usage of API that are not defined in win XP.

Motivation

At some point, I used function RegDeleteKeyEx function without noticing that this API is NOT available in win XP

Solution

So, I follow this post: Modifying WINVER and _WIN32_WINNT and declared this:

#include <winsdkver.h>
#define _WIN32_WINNT        0x0501         
#define WINVER              0x0501 

in file targetver.h

I was hoping that after this fix, when I compile my project that contains usage of RegDeleteKeyEx function, I will get compilation error. But I didn't.

QA

I tried looking for other, new WINAPI in vista, and just added a call to GetTickCount64 function. when compiling, I got this:

error C3861: 'GetTickCount64': identifier not found

which confirmed my solution.

Question

I've noticed that for RegDeleteKeyEx function, the Minimum supported client is Windows Vista, Windows XP Professional x64 Edition

However, my app will run in XP 32 as well.

How may I enforce compilation error in such use case?


Solution

  • Unfortunately, as I can see in "winreg.h", there is no conditional compilation of RegDeleteKeyEx (other than RegDeleteKey). So there is no (easy) way to trigger compilation error in this case.

    The only option (for the regular statical DLL loading) would be to create your own wrapper over winreg.h (or windows.h) which would handle the version check (and e.g. #undef RegDeleteKeyEx in the corresponding cases).


    Sometimes this kind of stuff is also solved using dynamic DLL loading (LoadLibrary / GetProcAdress) where you can check the presence of the particular function on the current version of Windows, where the application runs (so you could for example create a RegDeleteKey wrapper which would call RegDeleteKeyEx on the windows versions which support it and RegDeleteKey if run under versions which don't). The check of the feature presence is then done at runtime, so the program can run on any version of system and still use the newest features on the versions which support them (and doesn't have DLL "unresolved import" loading issues on the lower versions which do not support the feature).