Reputation: 119
I have a windows application that should run in XP and above. But Windows API for XP is usually different from vista and above. What conditional compilation flags can I check to determine which windows OS the code is being compiled for? If not, is there any way to achieve this?
I think it should look something like this:
void SomeClass::SomeFunction()
{
// Other code
#ifdef WINXP
windowsXPCall();
#else
WindowsCall();
#endif
// Other code
}
Upvotes: 3
Views: 1138
Reputation: 275385
With one executable, detecting "what version you are being targetted to" does not make much sense; your executable has to target every version. #ifdef
like the above cannot fix your problem, unless you build more than one executable (or use a helper dll which you build more than one version of).
Your problem may be solved by "how do I restrict the API available to winXP". @dxiv's answer solves that problem.
Upvotes: 0
Reputation: 17638
The answer depends on the compiler and toolchain. If the question is about Visual C++ then you should tag it as such.
For VC++ the target minimum OS version supported is controlled by a set of macros described at Using the Windows Headers.
In the simplest case, to target XP you would need to add
#define _WIN32_WINNT 0x0501
before including any of the windows headers, or add /D_WIN32_WINNT=0x0501
to the compiler command line.
Replace 0x0501
with 0x0502
if you don't need to support the original release of XP (without any service packs applied), per this note in the same linked article.
Note that some features introduced in the latest version of Windows may be added to a service pack for a previous version of Windows. Therefore, to target a service pack, you may need to define _WIN32_WINNT with the value for the next major operating system release. For example, the GetDllDirectory function was introduced in Windows Server 2003 and is conditionally defined if _WIN32_WINNT is 0x0502 or greater. This function was also added to Windows XP with SP1. Therefore, if you were to define _WIN32_WINNT as 0x0501 to target Windows XP, you would miss features that are defined in Windows XP with SP1.
P.S. It's you who controls the sources and compilation flags of your own code, so I am not sure how to answer this part of the original question.
What conditional compilation flags can I check to determine which windows OS the code is being compiled for?
Upvotes: 2