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

1.3k views Asked by At

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?

2

There are 2 answers

0
CristiFati On BEST ANSWER

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):

  • BOOL is defined in minwindef.h (#157: typedef int BOOL;)

  • Psapi.h only includes one file (#27: #include <winapifamily.h>)

    • winapifamily.h doesn't include any other file

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;
}
0
user20560078 On

to answer the question, I know this is DATED but the issues persist today. You need the following:

#include "stdafx.h"
#include <stdbool.h>
#include <Windows.h>
#include <stdlib.h> 
#include <psapi.h>

After stdlib.h was included, the errors were gone.