-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerutils.cpp
More file actions
73 lines (55 loc) · 1.9 KB
/
powerutils.cpp
File metadata and controls
73 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "powerutils.h"
#include <codecvt>
#include <locale>
#include <powersetting.h>
#include <powrprof.h>
// get friendly name from GUID as string
std::string PowerUtils::getFriendlyNameFromGUID(GUID guid) {
UCHAR buffer[2048];
DWORD bufferSize = sizeof(buffer);
DWORD result;
// PowerReadFriendlyName should return ERROR_SUCCESS if it got the name
result = PowerReadFriendlyName(NULL, &guid, &NO_SUBGROUP_GUID, NULL, buffer, &bufferSize);
if (result == ERROR_SUCCESS) {
// convert the wstring into a regular string (TODO: might use wstring later?)
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes((wchar_t *) buffer);
}
return ""; // couldn't get name
}
// return array of GUIDs
std::vector<GUID> PowerUtils::getPowerPlanGUIDs() {
std::vector<GUID> guids;
GUID guid;
DWORD bufferSize = sizeof(guid);
DWORD index;
for (index = 0;; index++) {
// zero out the guid var (idk i just saw somewhere that i might need to)
ZeroMemory(&guid, sizeof(guid));
// append GUIDs to vector by iterating through PowerEnumerate
if (PowerEnumerate(NULL, NULL, NULL, ACCESS_SCHEME, index, (UCHAR *) &guid, &bufferSize)
== ERROR_SUCCESS) {
guids.push_back(guid);
}
else {
break;
}
}
return guids;
}
// set power plan to GUID (true -> success, false -> failed)
bool PowerUtils::setPowerPlan(GUID guid) {
DWORD result;
result = PowerSetActiveScheme(NULL, &guid);
if (result == ERROR_SUCCESS)
return true;
return false; // error setting power plan
}
GUID PowerUtils::getActivePowerPlan() {
GUID *guidPtr;
DWORD result;
result = PowerGetActiveScheme(NULL, &guidPtr);
if (result == ERROR_SUCCESS)
return *guidPtr;
return GUID_NULL; // couldn't get active power plan, ret null
}