what is SDKDDKVer.h for? - c++

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 ?

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.

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..
SDKDDKVer.h applies default values unless the macros have already been defined
the following code can be used to explicitly define the macros
#define _WIN32_WINNT 0x0601
#define NTDDI_VERSION 0x06010000
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
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#
Update WINVER and _WIN32_WINNT
Using the Windows Headers
What does the Target Platform Version mean for a VS C++ project?

Related

Is there a difference between use of _WINDOWS_ and _WIN32 / _WIN64 macros?

Is there a difference between:
#if defined(_WIN32) || defined(_WIN64)
and:
#ifdef _WINDOWS_
I know that it is necessary to use defined() when there is more than 1 condition.
Yes, there is a difference. The _WIN32 and _WIN64 tokens are defined (conditionally) depending on what platform is being targeted. The former (_WIN32) will be defined for x86, x64, ARM and ARM-64 architectures; the latter (_WIN64) will be defined only for the two 64-bit architectures.
However, the _WINDOWS_ macro will always be defined if the "Windows.h" header file is included by a given source file. It is used (in that header) as a 'guard', to prevent multiple inclusions of its body. A very trimmed-down version of that header is:
/* ...
Copyright (c) Microsoft Corporation. All rights reserved.
...
Master include file for Windows applications.
--*/
#ifndef _WINDOWS_
#define _WINDOWS_
// ...
// <body of Windows.h>
// ...
#endif /* _WINDOWS_ */
Also, when using the MSVC compiler (and, possibly, some others), the _WIN32 and _WIN64 macros are predefined by the compiler (i.e. no header needs to be included for them to be defined for their relevant target platforms).
Just to add to the confusion, MSVC also adds a _WINDOWS macro (note the lack of the trailing underscore) by default, when creating new projects that target Windows (this is not formally predefined, but is one of the default, per-project "Preprocessor Definitions" in the projects' properties.

Redefining _WIN32_WINNT while using precompiled headers

Hi I currently have a problem redefining _WIN32_WINNT even though I have
#define _WIN32_WINNT 0x0600
#define WINVER _WIN32_WINNT
in my stdafx.h it still defaults to 0x0603
I'm currently using DDSTextureLoader that came with DirectX Tutorials and by beeing 603 it uses CreateFile2 and not CreateFile which creates an error :(
Remove all the #defines in the header files, and add the preprocessor definitions -D_WIN32_WINNT=0x0600;-DWINVER=_WIN32_WINNT in the project configuration. This makes sure that the definitions take effect at the very beginning of the compiling process. For the details, see here.
If it's not appropriate to remove the #defines in the headers. You may change them to the following:
#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0600
#endif
#ifndef WINVER
# define WINVER _WIN32_WINN
#endif

windows.h and MFC

Why can't I include windows.h in afx(MFC) projects?
Typically, MFC application code includes afx.h or afxwin.h (the latter includes former). First two lines of windows.h are
#ifndef _WINDOWS_
#define _WINDOWS_
which means that _WINDOWS_ becomes defined if this header is included.
Afx.h includes afxver_.h and this header includes afxv_w32.h which contains following code:
#ifdef _WINDOWS_
#error WINDOWS.H already included. MFC apps must not #include <windows.h>
#endif
...
#include <windows.h>
So, if you include windows.h before MFC headers, you'll get this error generated in compile time and, as you can see, if you include afxwin.h you don't need to include windows.h yourself - it will already be included by afxv_w32.h.
Because in MFC you are not supposed to use it directly. AFAIR you should include afx.h instead, which in turn indirectly includes windows.h the proper way.
You can include windows.h; but you need to first include afx.h (or similar). If you got the error: "MFC apps must not #include <Windows.h>"; it is from including something like afx.h after including windows.h.
You might need to turn on 'show includes' if not sure how it got included.

Where is WIN32 defined, and how can I include this definition in my project?

I am including a third party header and source file into my project.
At the top of the header there is this:
#if defined(WIN32) || defined(WIN16)
#ifndef MSDOS
#define MSDOS
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#ifndef MSDOS
#include <unistd.h>
#endif
#include "des.h"
The problem is that #if defined(WIN32) fails and the compilation fails when trying to #include unistd.h which I don't want to do.
I have third party project that works with this header file i.e. WIN32 is defined and it doesn't try to include In Visual Studio I did "Go To Definition" on "WIN32" and was taken to the following definition in WinDefs.h.
#define WIN32
I'm not sure this is where its getting WIN32 definition from, as the third party project does not appear to include "WinDefs.h".
So my problem is, how can I get WIN32 to be defined in my current new project?
Depends on your project setup. WIN32 is defined inside the windows header files, but you can pass it to the compiler as well ("-DWIN32" for gcc for example). Try it and see whether it compiles.
Visual Studio has the built-in define _WIN32. mingw-gcc has WIN32 and _WIN32 built-in so the project was likely tested using gcc. You might add
#if defined(_WIN32) && !defined(WIN32)
#define WIN32
#endif
or just add a -DWIN32 to the CFLAGS.
Check your includes. I am guessing that the third party header is included prior to the windows.h. So, in your main.cpp or equal it should be
#include <windows.h> // this will also include windefs.h
#include <thirdParty.h>
and not the other way around.
Hope that helps.
You can simply include the windows header files (windows.h) before including the third party header - as you already found out WIN32 is defined there but technicaly it could be defined anywhere (so if the third party project is not including the windows headers check if it's being defined in the compiler project settins directly).
BTW there is also a _WIN32 define that is set by the compiler, it's possibly a better idea to look for this define if checking if the code is being compiled under windows;
For those seeking answers to the
where is WIN32 defined
part of the questions, I've found it defined in:
minwindef.h
ole2.h
Note, I have no confidence that these are the only places it's defined. I expect there are probably other files where it's defined. Nevertheless, I thought this might help some people.
Some WIN32 defined in the compiler . Just like this,If you use the gcc for windows , WIN32 is defined . If you use the gcc for linux , WIN32 is not defined :)
So , the macros is a switch. You can define it to use somethine , and not define it to unuse something.

win32 C++ printing PRINTDLGEX not declared?

I am trying to figure out how to print in C++. I want to get the device context using the PrintDlgEx function, which needs a PRINTDLGEX structure. However, I cannot create a PRINTDLGEX because it says it's undeclared. I have included the Commdlg.h and Windows.h and linked the Comdlg32.lib, but all to no avail. Is there something I'm missing? I can go into the Commdlg header file and see that PRINTDLGEX is declared, but for some reason I can't use it? My operating system is Window Vista.
It's probably undeclared because it's within a #ifdef STDMETHOD block starting on line #878 of Commdlg.h
STDMETHOD is defined in basetyps.h
This post, Customizing PrintDlgEx and IPrintDialogCallback, might be useful as well.
You need to declare your target Windows version to be modern enough to support the structure, the defaults assume something ancient. Typically this will be done in your stdafx.h file. These definitions must come before the include of the Windows header files.
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
#define _WIN32_IE 0x0501