-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Power.pas
139 lines (111 loc) · 3.57 KB
/
NtUtils.Power.pas
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
unit NtUtils.Power;
{
This module provides support to power management-related APIs.
}
interface
uses
Ntapi.ntpoapi, Ntapi.WinNt, NtUtils, DelphiApi.Reflection;
// Issue a power information query/set request
function NtxPowerInformation(
InfoClass: TPowerInformationLevel;
[in, ReadsFrom] InputBuffer: Pointer;
[in, NumberOfBytes] InputBufferLength: Cardinal;
[out, WritesTo] OutputBuffer: Pointer;
[in, NumberOfBytes] OutputBufferLength: Cardinal
): TNtxStatus;
// Query the processsor power information
function NtxQueryProcessorPower(
out Info: TArray<TProcessorPowerInformation>
): TNtxStatus;
// Create a regular/PLM power request object
function NtxCreatePowerRequest(
out hxPowerRequest: IHandle;
UsePLM: Boolean = False;
[opt] const Reason: String = ''
): TNtxStatus;
// Enable/disable a power request
// - Regular requests don't use the process handle parameter
// - PLM requests require a process handle and an execution request type
function NtxActivatePowerRequest(
const hxPowerRequest: IHandle;
RequestType: TPowerRequestTypeInternal;
[opt, Access(PROCESS_SET_LIMITED_INFORMATION)] const hxProcess: IHandle = nil;
Enable: Boolean = True
): TNtxStatus;
implementation
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
uses
Ntapi.ntexapi, Ntapi.ntstatus, NtUtils.System, NtUtils.Objects,
DelphiUtils.AutoObjects;
function NtxPowerInformation;
begin
Result.Location := 'NtPowerInformation';
Result.LastCall.UsesInfoClass(InfoClass, icPerform);
Result.Status := NtPowerInformation(InfoClass, InputBuffer, InputBufferLength,
OutputBuffer, OutputBufferLength);
end;
function NtxQueryProcessorPower;
var
SystemInfo: TSystemBasicInformation;
Buffer: IMemory<PProcessorPowerInformationArray>;
i: Integer;
begin
// We need to know the number of processors
Result := NtxSystem.Query(SystemBasicInformation, SystemInfo);
if not Result.IsSuccess then
Exit;
IMemory(Buffer) := Auto.AllocateDynamic(SystemInfo.NumberOfProcessors *
SizeOf(TProcessorPowerInformation));
// Issue the reuqest
Result := NtxPowerInformation(ProcessorInformation, nil, 0, Buffer.Data,
Buffer.Size);
if not Result.IsSuccess then
Exit;
// Capture the result
SetLength(Info, SystemInfo.NumberOfProcessors);
for i := 0 to High(Info) do
Info[i] := Buffer.Data{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF};
end;
function NtxCreatePowerRequest;
var
InfoClass: TPowerInformationLevel;
Input: TCountedReasonContext;
hPowerRequest: THandle;
begin
if UsePLM then
InfoClass := PlmPowerRequestCreate
else
InfoClass := PowerRequestCreate;
// Prepare the reason structure
Input := Default(TCountedReasonContext);
Input.Version := DIAGNOSTIC_REASON_VERSION;
if Reason <> '' then
begin
Input.Flags := DIAGNOSTIC_REASON_SIMPLE_STRING;
Result := RtlxInitUnicodeString(Input.SimpleString, Reason);
if not Result.IsSuccess then
Exit;
end
else
Input.Flags := DIAGNOSTIC_REASON_NOT_SPECIFIED;
// Issue the request
Result := NtxPowerInformation(InfoClass, @Input, SizeOf(Input),
@hPowerRequest, SizeOf(hPowerRequest));
if Result.IsSuccess then
hxPowerRequest := Auto.CaptureHandle(hPowerRequest);
end;
function NtxActivatePowerRequest;
var
Input: TPowerRequestAction;
begin
Input := Default(TPowerRequestAction);
Input.PowerRequestHandle := HandleOrDefault(hxPowerRequest);
Input.RequestType := RequestType;
Input.SetAction := Enable;
Input.ProcessHandle := HandleOrDefault(hxProcess);
Result := NtxPowerInformation(PowerRequestAction, @Input, SizeOf(Input),
nil, 0);
end;
end.