0

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.

Ian
  • 675
  • 1
  • 9
  • 25
  • 2
    not confuse compiler and linker errors, this is absolute different things. `LNK2019` for `GetProcessMemoryInfo` say that you not include `Psapi.lib` or use old sdk – RbMm Jan 11 '18 at 14:31
  • Yea, that's the problem, when I include Windows.h in its entirety I get a multiply defined issue. When I define lean/mean, it automatically removes the child headers that I need. I can't currently find a third option or middle ground to fix it. @RbMm. I do understand that they are vastly different errors, but they stem from include issues. – Ian Jan 11 '18 at 14:34
  • 1
    @RbMm +1 because I think you are on the right track. However, I'm afraid "that you not **include** `Psapi.lib`" is bad (or should I say "unlucky") wording... ;-) – Scheff's Cat Jan 11 '18 at 14:36
  • @Scheff Apparently Psapi.lib / Psapi.h does not function without having a Windows.h included *before* it (I tried changing the order too, and that was even worse). Psapi.h depends on Windows.h apparently. – Ian Jan 11 '18 at 14:40
  • 1
    Sorry, I was joking. RbMm points out that you get a **link** error. To spell it out: Your `#include`s are probably OK, your fix with `#define WIN32_LEAN_AND_MEAN` as well. If you get a link error, please, check whether the **lib** file `Psapi.lib` is in the library dependencies. If it is, it could be an old version... ...and you need an updated SDK to use it. – Scheff's Cat Jan 11 '18 at 14:43
  • Ah, ok, I wasn't sure if that was what you meant. Time to try fixing it that way. – Ian Jan 11 '18 at 14:48
  • The sdk is up to date and the libraries (Psapi.lib) exist. Trying to figure out *how* to check if it is in the library dependencies. – Ian Jan 11 '18 at 14:55
  • 1
    that you have `GetProcessMemoryInfo` say about you use `PSAPI_VERSION==1` (otherwise will be used `K32GetProcessMemoryInfo` name). the `GetProcessMemoryInfo` exist only in `psapi.lib` which you need include as linker input – RbMm Jan 11 '18 at 15:01
  • @RbMm Ok. Makes sense. Looking at the actual psapi.h right now and I see those if version > 1 statements. My version seems to be 1, as the K32 versions are not defined no matter what I do. Still getting the unresolved symbol for the normal GetProcessMemoryInfo. Taking another look at the linker. – Ian Jan 11 '18 at 15:15
  • 1
    if you include `psapi.lib` - you never got error about unresolved `GetProcessMemoryInfo` or `K32GetProcessMemoryInfo` (inpedend from version) – RbMm Jan 11 '18 at 15:16
  • Finally got it guys, thanks! – Ian Jan 11 '18 at 16:16

1 Answers1

4

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!

Ian
  • 675
  • 1
  • 9
  • 25
  • Nevertheless, you solved it. Do you know that you can [accept your own answer](https://stackoverflow.blog/2009/01/06/accept-your-own-answers/)? (It becomes active after 48 h.) – Scheff's Cat Jan 11 '18 at 16:55
  • Yea @Scheff, gotta wait another couple of days first. Thanks for the help! – Ian Jan 11 '18 at 17:06