c++windowsincludepsapi

How would I include Psapi.h without running into a multiply included error?


Currently running into an issue where Winsock.h is already included in a header file elsewhere in my project...

Header not directly included:

# if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
# error WinSock.h has already been included
# endif // defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)

So that when I try to build, I get a the error "C1189 Winsock.h has already been included" (from the defined error message in the above piece of code).

However, I need to be able to use parts of the PSAPI to obtain memory usage information about the current process. I tried to include in my other file...

My .cpp File:

#include <Windows.h>
#include <Psapi.h>

...

SIZE_T getMemoryInfo() {    // The function that needs the includes

    PROCESS_MEMORY_COUNTERS pmc;
    SIZE_T memoryUsed;

    GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
    memoryUsed = pmc.WorkingSetSize;

    return memoryUsed;

}

After researching I then tried using WIN32_LEAN_AND_MEAN to try to get rid of the Winsock.h include, which works, but now when I try to use PROCESS_MEMORY COUNTERS I get an undefined symbol error...

"LNK2019 unresolved external symbol GetProcessMemoryInfo referenced in function..." and "LNK1120 1 unresolved externals [in myFile]"

I expect that I need to figure out how to include the actual header files I need, but the Windows docs do not say (and Wikipedia says that most of the child headers cannot be individually included, so that's probably an issue).

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>    // Windows specific libraries for collecting software metrics.
#include <Psapi.h>

I've been trying to figure this out for a full day, any help would be appreciated as Windows Docs are pretty unhelpful and I am thoroughly frustrated. I apologize if it is a stupid question, but I frequently struggle with includes.


Solution

  • Finally figured it out.

    Apparently the psapi.lib wasn't automatically being linked to by Visual Studio (despite the fact the Windows util libs are...). You have to go into properties/linker/input and manually add the library as a dependency. If it can't find the library location, add directory manually in C/C++ as a search directory.

    You do have to have #define WIN32_LEAN_AND_MEAN as well.

    Hope that helps, if anyone needs help with this in the future, just comment here.

    Credit for the solution goes to @RbMn and @Scheff, thanks guys!