Why including <Windwos.h> is not working?

moaz rashad 21 Reputation points
2021-07-07T15:52:50.887+00:00

I am new to windows programming. I was following this guide on learning how to create windows program

but I'm having a problem with including Windows.h It doesn't seem to do anything.
for example when I try to use any variable type defined in this header like HINSTANCE or use functions like GetTimeZoneInformation defined in timezoneapi.h which should be included within Windows.h

But, when I explicitly include any child header like timezoneapi.h individually it works.

for example

#include <Windows.h>  
  
int main()  
{  
 TIME_ZONE_INFORMATION New;  
 GetTimeZoneInformation(&New);  
}  

the above code gives me a lot of undefined errors

but this code

#define _X86_  
#include <timezoneapi.h>  
  
int main()  
{  
 TIME_ZONE_INFORMATION New;  
 GetTimeZoneInformation(&New);  
}  

compiles and runs successfully.

I am using VS2017 version 15.9.36 With Windows 10 SDK (10.0.17134.0) My installed Packages

Edit:
I tried using VSCode to use Windows.h with this extension and It compiles fine in the cmd with g++ version 5.3.0

I hope it gives some info about where exactly is the problem.

Edit2:
I included the Windows.h file of Mingw that I tried in VSCode and it worked (with needing to explicitly define X86 and I get a lot of the same warning referring to VS "Windows.h" warning C4821: Unable to determine Unicode encoding type, please save the file with signature (BOM))
so the problem is in the VS file.
so When I opened both and compared them (They were a little different) I found out that there were preprocessor definitions conditions that prevented nearly all the includes in the file.

There were a lot of conditions there about RC_INVOKED so I couldn't figure out which condition exactly caused it.
I would be glad if anyone can help figure out what is causing the problem in the file as I don't know if I can post anything from it here as it is copyrighted.

I would like to thank all the people that tried to help me.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,649 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,431 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,547 questions
{count} votes

1 additional answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-07-07T18:32:26.907+00:00

    The error message says Unicode encoding type and it says BOM. So yes the error C4821 is the problem; see Compiler Warning (level 1) C4821. See How to: Save and open files with encoding. The following shows what one of my files has, it is the default.

    112606-t.jpeg

    What appears to be happening is that there is a couple (a few or whatever) bytes at the beginning of the file that you do not see that are confusing VS; or maybe it is the absence of that. So when you put the #define _X86_ at the beginning of the file that makes it possible for VS to use the #include after it. Probably if you put a comment at the beginning then #include <Windows.h> after that it would work. A better solution is to save the file so that VS is not confused.