c++windowspsapi

Why does changing the order of including psapi.h gives compilation erros?(Indentifier BOOL is undefined)


I am using Visual Studio Community 2017 to code c++. When I run the following code everything works fine.

#include "pch.h"
#include<Windows.h>
#include<Psapi.h>
#include <iostream>
#include <conio.h>

int main()
{

    std::cout << "Really!! How do you do it?";
    _getch();
}

But if I change the order of #includes by including psapi.h before Windows.h, compiler goes badass and throws 198 errors at me, which surprisingly(maybe only to me) includes Identifier "BOOL" is undefined. Why is this happening?


Solution

  • Since Psapi.h's include tree is trivial, I'm going to exemplify.
    Everything relies on VStudio 2015 (Community) (v14.0.25431.01 Update 3) and Windows Kits 8.1 (? funny, because v10 is there too) files (with default env vars and preprocessor definitions):

    So, when reaching Psapi.h (#87: BOOL WINAPI EnumProcesses (...), the compiler doesn't know anything about BOOL, so it complains.

    Windows.h includes minwindef.h (indirectly, via windef.h), and that's why it works when you include it before Psapi.h.

    Personally, I think it's a bug in Psapi.h, since it's not self contained, but there might be a good reason (that I'm not aware of) for that.
    Anyway, if this is indeed a bug, it wouldn't be MS's 1st one :)

    #include <Windows.h>
    #include <WinSock2.h>
    
    // main present just for rigorosity's sake
    int main() {
        return 0;
    }