DirectXTK - The procedure entrypoint CreateFile2 could not be located in the kernel32.dll - c++

So I wanted to try using DirectXTK's spritefont class and after including all of the needed files I end up with an error poping up as the program is about to start, telling me that The procedure entrypoint CreateFile2 could not be located in the kernel32.dll. Now I have searched around for the problem and from what I can tell it seems the directxtk is trying to use windows 8 specific things (or possibly 8.1 I guess) while I am using windows 7. The general fix that people seem to suggest is to change what kind of version is used by defining
#define WINVER 0x0601
#define _WIN32_WINNT 0x601
to make it use the windows 7 version. I put those defines in a header file as msdn says, but I still get the same error. If it matters I am coding it in c++ in visual studio 2013.

The DirectX Tool Kit supports a broad array of Microsoft platforms and OS configurations, so the first step is to make sure you pick the correct VCXPROJ and/or NuGet package for your intended project and Visual C++ compiler edition.
For Windows desktop applications using VS 2013, you should use:
DirectXTK_Desktop_2013.vcxproj
-or- NuGet package directxtk_desktop_2013
The coding techniques used in the library are covered in great detail in this article Dual-use Coding Techniques for Games.
The primary control used throughout the code-base is the Windows OS target version which is set by _WIN32_WINNT as per MSDN. With the Windows 8.1 SDK (included with VS 2013) the SDK will default to using _WIN32_WINNT=0x0603 / _WIN32_WINNT=_WIN32_WINNT_WINBLUE which means to build for a Windows 8.1 or later OS. This is why the code uses CreateFile2 which is required for the code to be usable for Windows 8.x Store, Windows phone 8.x, or Windows 10 UWP.
If, however, you use the Windows desktop projects, they build with _WIN32_WINNT=0x0600 / _WIN32_WINNT=_WIN32_WINNT_VISTA and the library will make use of the APIs that work on Windows Vista SP2+KB971644 (needed for Direct3D 11 and Windows 7.
In the Direct3D Win32 Game Visual Studio template, I set this value in pch.h before including any other headers.
#include <WinSDKVer.h>
#define _WIN32_WINNT 0x0600
#include <SDKDDKVer.h>

The CreateFile2 reference comes from https://github.com/microsoft/DirectXTK/blob/master/Src/LoaderHelpers.h here:
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile(safe_handle(CreateFile2(fileName,
GENERIC_READ,
FILE_SHARE_READ,
OPEN_EXISTING,
nullptr)));
#else
ScopedHandle hFile(safe_handle(CreateFileW(fileName,
GENERIC_READ,
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr)));
#endif
So if the header is compiled with _WIN32_WINNT <= 0x0601, it should not use CreateFile2. How did you try to override it? I used the VS "Preprocessor Definitions" field and was able to force the else path. I recommend you double check your override.

Related

Microsoft Visual C++ Target O/S

I am working on a .NET application that is using C++ libraries. One library is a C++/CLR library that uses another C++ library. The application that uses these libraries is required to operate at least on Windows 7. The system is compiled using Visual Studio 2019, and is using the latest SDK. From what I've read, using the latest SDK is possible if targeting at minimum Windows 7.
When I launch the application on Windows 7, I get the following error:
A procedure imported by 'XXX.Net.dll' could not be loaded.
Running on Windows 10 causes no problems.
XXX.Net.dll is the C++/CLR library which uses the C++ library.
I read that setting the WINVER and _WIN32_WINNT macros would explicitly target the version that is wanted. I've added these definitions based on what Microsoft indicated:
to modify the macros, in a header file (for example, in targetver.h
So, within my C++ library, I added the following macro declaration to targetver.h:
#define WINVER 0x0601
#define _WIN32_WINNT 0x0601
From what I understand, this should allow the library to operate properly on Windows 7. Can someone tell me why this would not work?
Note: we are using Ninject for packing all binaries into the main binary. And this error occurs in the line:
new StandardKernel(new Bindings().
OK -- I had decided to test the library failing directly -- and found that the library was accessing an SDK operation not available on Windows 7 - which was causing the library to fail.

Are Windows executables forward and backwards compatible?

Say I'm building a C++ portable executable and I include the windows.h header. Also, let's stick to just Windows 10. If I use the most recent Windows 10 Visual C++ toolchain to build the binary, can I run the binary on earlier versions of Windows 10? If I use the earliest Windows 10 Visual C++ toolchain to build the binary, can I run it on the latest version of Windows 10?
Let me know if you would like me to clarify the question.
Quoting from Microsoft's documentation under Update WINVER and _WIN32_WINNT.
Visual Studio and the Microsoft C++ compiler support targeting Windows 7 SP1 and later.
Older toolsets include support for Windows XP SP2, Windows Server 2003 SP1, Vista, and Windows Server 2008.
Windows 95, Windows 98, Windows ME, Windows NT, and Windows 2000 are unsupported.
Applications targeted at Windows 10 with the default #define's will run under all versions of Windows 10.
#define WINVER 0x0A00
#define _WIN32_WINNT 0x0A00
To use newer APIs introduced in a later version of Windows 10, see the section under Remarks starting with "for a more fine-grained approach to versioning, you can use the NTDDI version constants in sdkddkver.h", and also the Using the Windows Headers page. For example, compiling with #define _WIN32_WINNT 0x0A00 and #define NTDDI_VERSION NTDDI_WIN10_19H1 will allow using features introduced in Windows 10 19H1, but only targets Windows 10 19H1 and later.

Windows SDK Version setting in Visual Studio 2017

I have a c++ project that compiles well under Visual Studio 2013.
Today I installed Visual Studio 2017 Professional Edition, then there's a new setting in project settings > General called "Windows SDK Version", by default is 10.0.16299.0. Since I'm compiling windows desktop programs for targeting Windows 7 systems, I changed it to 8.1, is this correct?
Generally speaking, a Windows SDK supports its "main" version and also the previous ones, but you need to specify what Windows version your program will need. In fact, you're better off doing so or else you can inadvertently use features not available in the version you want to support.
Given an SDK, you indicate which older Windows version to target by defining the WINVER and _WIN32_WINNT macros somewhere in your project files or in the C/C++ Preprocessor project settings in Visual Studio.
For example, the following definitions target Windows 7:
#define WINVER 0x0601
#define _WIN32_WINNT 0x0601
For more information, see Using the Windows Headers and Modifying WINVER and _WIN32_WINNT
Indeed I raised this issue because my freshly installed Visual Studio could not build the VM because SDK 16299 is now indeed the default. It's mentioned here:
https://en.wikipedia.org/wiki/Microsoft_Windows_SDK.
.
Also MS does not make finding older SDK's very easy. You have to click through to another page all the way on the end of this page:
https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk
Even though I googled on "Microsoft Windows SDK 15063".
.
So all-in-all it's now a small chore for newbies to get up and running on the VM. To start, I think it should be made as easy as possible. (Complexity will come soon after that :)).
.
PS I'm not sure about Windows 7 compatibility. But the current VM SDK is also listed as being for Windows 10.

Visual C++ application won't run on windows 7

I am using Visual Studio Community 2015 and I wrote some simple Win32 demo application which should download file from internet and execute two HTTP GET requests.
I am using functions like InternetOpenA, InternetConnectA, HttpOpenRequestA, URLDownloadToFile, etc.
Only thing that I have changed in settings is Platform Toolset to Visual Studio 2013 (v120) and my targetver.h file looks like this:
#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 <WinSDKVer.h>
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#include <SDKDDKVer.h>
However, it runs on my Windows 10 computer, but it does not run on windows 7. It says: Missing MSVCR120.dll file. I can install appropriate C++ Redistributables but that is not solution that I need.
Is there any other options I need to include when I compile so I can avoid this error?
To avoid your application needing a separate runtime DLL, in the project settings, look under:
C/C++ > Code Generation > Runtime Library
and choose multi-threaded, rather than multi-threaded DLL.
You do not need to change the platform toolset.
In my project, that I compile with VS 2017 and want to run all the way down to Vista, I do the same as you, but without the first #include <WinSDKVer.h>. I just set the _WIN32_WINNT macro to 0x0600 and so far it's working fine.
I target the Windows 8.1 SDK, and use MFC, if that helps.
I've used dependency walker in the past to diagnose dll dependencies. Hopefully it's something silly like the 32-bit or 64-bit runtimes being missing.
As keith recommended in his answer, you can also try static linking the vc runtime (/MT[d] under C++/Code generation/Runtime Library) so that it doesn't need to load the runtime as a dll. Note that this is not the recommended option, since the VC runtime cannot be patched by Windows Update if it's burned into your executible.

Explorer thumbnails (winXP)

I have a file format I need to be able to show in explorer thumbnails. Since the target system is windows XP, the Vista PreviewHandler API will not be suitable. Ill be using c++.
How would I do it?
You'll need to register a shell extension for your file type. The extension contains code that extracts/generates the thumbnail by implementing the IExtractImage interface.
See: IExtractImage Interface on MSDN
https://github.com/reliak/moonpdf/tree/master/ext/sumatra/src/previewer is perfect example.
To build both x86 and x64 versions of DLL I use VS 2010 with SP1 along with Win7 x64 SDK (for <thumbcache.h>) installed on Windows 7 x64. Builded DLLs works fine on Win7 and Win10 of either bitness.
Also don't forget /MD linker flag to avoid necessity to install Microsoft Visual C++ 2010 Redistributable Package (x86) on WinXP.
Next lines may be added to "targetver.h" to avoid import and using of new functions from newer versions of Windows system DLLs (say, there is no RegDeleteTreeW in WinXP's advapi.dll):
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#include <winsdkver.h>
#include <SDKDDKVer.h>
Additionally it has implementation of previewer for some file formats, but related interfaces can be completely omitted in your implementation.