2

I would like to use Visual Studio 2019 to enjoy the latest C++ additions, but targeting Windows 7.

I created a Windows C++ application using the VS 2019 wizard (running on Windows 10).

A targetver.h file is created by the wizard, with the following content:

#pragma once

// // Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>

I followed the instructions in the comment lines, and added the following lines intargetver.h (before the #include <SDKDDKVer.h> line):

// Target Windows 7 SP1
#include <WinSDKVer.h>
#define _WIN32_WINNT 0x0601

To test that, in the program's main function I invoked an API (PathCchAppend) that is not supported in Windows 7.

The program builds fine (I statically link the CRT), and runs fine in Windows 10.

The same program fails when executed in Windows 7, showing the following error message:

Error message when trying to run the VS 2019 C++ application on Windows 7

Now, the program should have not compiled at all, because I specified the Windows 7 target, and the aforementioned API is not available on Windows 7.

Is this a bug in the Windows SDK?

Is it possible to target Windows 7 using VS 2019 and the Windows 10 SDK, getting errors during the build process when an API or structure that is not supported in Windows 7 is used in the code, and how?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • Does this answer your question? [Visual studio 2015 run-time dependencies or how to get rid of Universal CRT?](https://stackoverflow.com/questions/35805113/visual-studio-2015-run-time-dependencies-or-how-to-get-rid-of-universal-crt) – dewaffled Sep 13 '20 at 19:11
  • @dewaffled: I'll read the question you linked in details later, but if it's about static linking with the CRT, I already did that. To me, the code should not compile at all, as I tried to invoke an API that is not supported on Windows 7. – Mr.C64 Sep 13 '20 at 19:15
  • you try use api set (*api-ms-win-core-path-l1-1-0*) and api (*PathCchAppend*) which simply not exist in *win7*. – RbMm Sep 13 '20 at 20:17
  • and no any conditional blocks in *pathcch.h* based on version selected. so compiled your code ok, build ok, but will be run only in win8+ – RbMm Sep 13 '20 at 20:20
  • @RbMm: So, what’s the point of #defining a preprocessor macro to explicitly target Windows 7? I thought I should get compile-time errors if I try to invoke APIs that are not available in Windows 7. – Mr.C64 Sep 13 '20 at 22:36
  • 2
    `PathCchAppend` was introduced in Windows 8. The header file it's declared in (`PathCch.h`) does not seem to have any tests for SDK versions at all, which is why your compile succeeds. Is this a bug in the SDK? I guess so. – Jonathan Potter Sep 13 '20 at 22:41

1 Answers1

1

VS 2019's toolset and the latest Windows 10 SDK support targeting Windows 7 Service Pack 1.

You have already configured the Windows headers in the Windows 10 SDK properly:

#include <winsdkver.h>
#define _WIN32_WINNT 0x0601
#include <sdkddkver.h>

You can still call APIs that are not supported by Windows 7 in this mode, which is why calling PathCchAppend builds, links, and fails at runtime.

The problem you are seeing may also be related to the Universal C/C++ Runtime not being on your target machine. Install the x86 and/or x64 native version on your target test machine.

See Microsoft Docs.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81
  • I statically linked to the CRT. Should I still install additional DLLs on Windows 7? Can't I just deploy a single EXE built with VS 2019 and statically linked to the CRT? – Mr.C64 Sep 13 '20 at 19:18
  • In addition, I would expect a build failure, as I tried (on purpose) to invoke an API that is _not_ supported on Windows 7. – Mr.C64 Sep 13 '20 at 19:30
  • problem not related to crt - *api-ms-win-core-path-l1-1-0* (aka *kernelbase* ) and *PathCchAppend* not crt – RbMm Sep 13 '20 at 20:14