23

All project created with MSVC have stdafx, which is precompiled headers, which I know what they are but what about targetver.h ? It includes SDKDDKVer.h, and I can't find what is that header about.

What is this for ?

jokoon
  • 6,207
  • 11
  • 48
  • 85

2 Answers2

13

targetver.h and SDKDDKVer.h are used to control what functions, constants, etc. are included into your code from the Windows headers, based on the OS that you want your program to support. I believe that targetver.h sets defaults to using the latest version of Windows unless the defines are specified elsewhere.

SDKDDKVer.h is the header file that actually defines the #defines that represent each version of Windows, IE, etc.

Andy
  • 30,088
  • 6
  • 78
  • 89
  • 4
    The `targetver.h` file is autogenerated when you create the project and only includes two lines: `#pragma once` and `#include `, so basically it does nothing that including SDKDDKVer.h on its own doesn't do.. – d7samurai Jan 12 '14 at 19:26
  • @d7samurai well it does nothing by default, but you can specify the target version in `targetver.h`, and you're supposed to do that before calling `SDKDDKVer.h`. It's just a file that allows you to do so. – The_Rafi Jul 10 '15 at 15:54
  • When you create a Windows Desktop application using a Visual Studio wizard, the resulting `.rc` resource file may include the following: `#ifndef APSTUDIO_INVOKED #include "targetver.h" #endif` In such applications it is best to maintain a `targetver.h` file to define `_WIN32_WINNT` and `WINVER`, rather than defining these within `stdafx.h`. – MikeOnline Jan 13 '20 at 18:39
3

Line 193 of the SDKDDKVer.h (in SDK 8.1) states:

"if versions aren't already defined, default to most current"

This comment is specifically referring to the _WIN32_WINNT and NTDDI_VERSION macros.

So..

  1. SDKDDKVer.h applies default values unless the macros have already been defined
  2. the following code can be used to explicitly define the macros
  • #define _WIN32_WINNT 0x0601
  • #define NTDDI_VERSION 0x06010000
  1. Interestingly enough, the SDKDDKVer.h header file has 'constant' values defined for all of the SDK versions. For example:
  • #define _WIN32_WINNT_WINXP 0x0501
  • #define _WIN32_WINNT_WIN7 0x0601
  • #define _WIN32_WINNT_WIN8 0x0602
  1. One convention is to define _WIN32_WINNT and NTDDI_VERSIONin a header file called TargetVer.h, which you would reference in your pre-compiled header StdAfx.h.

#ADDTIONAL READING#

AJM
  • 1,317
  • 2
  • 15
  • 30
Pressacco
  • 2,815
  • 2
  • 26
  • 45
  • Superb, usable answer! It is a pity that those constants are not available before including ``. – Liviu Jan 15 '18 at 12:27
  • 1
    @Liviu You don't need to include this header before. You can for example #define NTDDI_VERSION NTDDI_WIN10 even if NTDDI_WIN10 is not yet known to the preprocessor. – Johan Boulé Aug 11 '22 at 12:50