1

I just created a new project (Win32 Console Application) in visual studio 2017 RC, and coded this, that, as far as my understanding goes, should be working.

#include "stdafx.h"
#include <string>
#include <iostream>
#include <windows.h>
#include "PowrProf.h Or Powerbase.h"
using namespace std;
int main(int argc, _TCHAR* argv[])
{
    SYSTEM_POWER_POLICY PowerPlugged;
    SYSTEM_POWER_POLICY PowerUnPlugged;
    POWER_ACTION_POLICY ActionPlugged;
    POWER_ACTION_POLICY ActionUnPlugged;
    CallNtPowerInformation(SystemPowerPolicyAc, NULL, 0, &PowerPlugged, sizeof(SYSTEM_POWER_POLICY));
    CallNtPowerInformation(SystemPowerPolicyDc, NULL, 0, &PowerUnPlugged, sizeof(SYSTEM_POWER_POLICY));
    ActionPlugged.Action = PowerActionNone;
    ActionUnPlugged.Action = PowerActionNone;
    PowerPlugged.LidClose = ActionPlugged;
    PowerUnPlugged.LidClose = ActionUnPlugged;
    CallNtPowerInformation(SystemPowerPolicyAc, &PowerPlugged, sizeof(SYSTEM_POWER_POLICY), NULL, 0);
    CallNtPowerInformation(SystemPowerPolicyDc, &PowerUnPlugged, sizeof(SYSTEM_POWER_POLICY), NULL, 0);

    string yaes;
    getline(cin, yaes);
    return 0;
}

Now I'm sorry if this is a dumb question, but I really never had to deal with the Linker, and the code gives me no errors. Except for when building of course.

The remarks section of the documentation says nothing about this.

I am on windows 10 and I've tried including Powerbase.h, PowrProf.h. Both gave me the exact same errors:

LNK2019: unresolved external symbol _CallNtPowerInformation@20 referenced in function _main BatterySettings c:\Users\badmouthing\documents\visual studio 2017\Projects\BatterySettings\BatterySettings\BatterySettings.obj

And of course

LNK1120 1 unresolved externals BatterySettings c:\users\profanity(I'm just having fun, the username is the same)\documents\visual studio 2017\Projects\BatterySettings\Debug\BatterySettings.exe

So what am I doing wrong? Thank you.

TrisT
  • 639
  • 6
  • 19

1 Answers1

3

When you include PowrProf.h try doing it like this

extern "C" {
#include <Powrprof.h>

}
#pragma comment(lib, "Powrprof.lib")

If that is not it, have you tried doing a clean rebuild?

Nathan Wiley
  • 101
  • 6
  • Worked. Would you mind explaining how/why? – TrisT Feb 01 '17 at 12:18
  • 1
    @ninjaiceflame - you use [`CallNtPowerInformation`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa372675(v=vs.85).aspx) - so it must be implemented somewhere. if you read documentation - it implemented in `PowrProf.dll` - so you need link with `PowrProf.lib` – RbMm Feb 01 '17 at 12:26
  • 1
    @RbMm Got that part, What I don't understand is what `extern "c"` is for. I tried not using it and it worked. – TrisT Feb 01 '17 at 12:35
  • 2
    @TrisT: It tells the compiler to use C [linkage](http://en.cppreference.com/w/cpp/language/language_linkage) (vs. the standard C++ linkage when compiling C++). You don't need it, because the Windows API headers already specify C linkage throughout. That's what you get for [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). – IInspectable Feb 01 '17 at 13:26