-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
252 lines (225 loc) · 9.06 KB
/
Copy pathNativeMethods.cs
File metadata and controls
252 lines (225 loc) · 9.06 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace BatteryMonitor
{
internal static class NativeMethods
{
internal static readonly Guid GUID_DEVCLASS_BATTERY = new Guid(0x72631E54, 0x78A4, 0x11D0, 0xBC, 0xF7, 0x00, 0xAA, 0x00, 0xB7, 0xB3, 0x2A);
internal const uint IOCTL_BATTERY_QUERY_TAG = (0x00000029 << 16) | ((int)FileAccess.Read << 14) | (0x10 << 2) | (0);
internal const uint IOCTL_BATTERY_QUERY_INFORMATION = (0x00000029 << 16) | ((int)FileAccess.Read << 14) | (0x11 << 2) | (0);
internal const uint IOCTL_BATTERY_QUERY_STATUS = (0x00000029 << 16) | ((int)FileAccess.Read << 14) | (0x13 << 2) | (0);
internal const int DEVICE_INTERFACE_BUFFER_SIZE = 120;
internal const UInt32 BATTERY_UNKNOWN_TIME = 0xffffffff;
internal const int ERROR_INVALID_FUNCTION = 1;
internal const int ERROR_NO_MORE_ITEMS = 259;
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr SetupDiGetClassDevs(
ref Guid guid,
[MarshalAs(UnmanagedType.LPTStr)] string enumerator,
IntPtr hwnd,
DEVICE_GET_CLASS_FLAGS flags);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiEnumDeviceInterfaces(
IntPtr hdevInfo,
IntPtr devInfo,
ref Guid guid,
uint memberIndex,
ref SP_DEVICE_INTERFACE_DATA devInterfaceData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr hdevInfo,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
uint deviceInterfaceDetailDataSize,
out uint requiredSize,
IntPtr deviceInfoData);
[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetupDiGetDeviceInterfaceDetail(
IntPtr hdevInfo,
ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
IntPtr deviceInterfaceDetailData,
uint deviceInterfaceDetailDataSize,
out uint requiredSize,
IntPtr deviceInfoData);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr CreateFile(
string filename,
[MarshalAs(UnmanagedType.U4)] FileAccess desiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare shareMode,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FILE_ATTRIBUTES flags,
IntPtr template);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool CloseHandle(IntPtr hdevInfo);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool DeviceIoControl(
IntPtr handle,
uint controlCode,
[In] IntPtr inBuffer,
uint inBufferSize,
[Out] IntPtr outBuffer,
uint outBufferSize,
out uint bytesReturned,
IntPtr overlapped);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern bool DeviceIoControl(
IntPtr handle,
uint controlCode,
ref uint inBuffer,
uint inBufferSize,
ref uint outBuffer,
uint outBufferSize,
out uint bytesReturned,
IntPtr overlapped);
[Flags]
internal enum DEVICE_GET_CLASS_FLAGS : uint
{
DIGCF_DEFAULT = 0x00000001,
DIGCF_PRESENT = 0x00000002,
DIGCF_ALLCLASSES = 0x00000004,
DIGCF_PROFILE = 0x00000008,
DIGCF_DEVICEINTERFACE = 0x00000010
}
[Flags]
internal enum LOCAL_MEMORY_FLAGS
{
LMEM_FIXED = 0x0000,
LMEM_MOVEABLE = 0x0002,
LMEM_NOCOMPACT = 0x0010,
LMEM_NODISCARD = 0x0020,
LMEM_ZEROINIT = 0x0040,
LMEM_MODIFY = 0x0080,
LMEM_DISCARDABLE = 0x0F00,
LMEM_VALID_FLAGS = 0x0F72,
LMEM_INVALID_HANDLE = 0x8000,
LHND = (LMEM_MOVEABLE | LMEM_ZEROINIT),
LPTR = (LMEM_FIXED | LMEM_ZEROINIT),
NONZEROLHND = (LMEM_MOVEABLE),
NONZEROLPTR = (LMEM_FIXED)
}
[Flags]
internal enum FILE_ATTRIBUTES : uint
{
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline = 0x00001000,
NotContentIndexed = 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance = 0x00080000
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct SP_DEVICE_INTERFACE_DETAIL_DATA
{
public int CbSize;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string DevicePath;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SP_DEVICE_INTERFACE_DATA
{
public int CbSize;
public Guid InterfaceClassGuid;
public int Flags;
public UIntPtr Reserved;
}
internal enum BATTERY_QUERY_INFORMATION_LEVEL
{
BatteryInformation = 0,
BatteryGranularityInformation = 1,
BatteryTemperature = 2,
BatteryEstimatedTime = 3,
BatteryDeviceName = 4,
BatteryManufactureDate = 5,
BatteryManufactureName = 6,
BatteryUniqueID = 7
}
//https://learn.microsoft.com/de-de/windows/win32/power/battery-query-information-str
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct BATTERY_QUERY_INFORMATION
{
public uint BatteryTag;
public BATTERY_QUERY_INFORMATION_LEVEL InformationLevel;
public int AtRate;
}
[Flags]
internal enum BATTERY_CAPABILITIES : uint
{
BATTERY_CAPACITY_RELATIVE = 0x40000000,
BATTERY_IS_SHORT_TERM = 0x20000000,
BATTERY_SET_CHARGE_SUPPORTED = 0x00000001,
BATTERY_SET_DISCHARGE_SUPPORTED = 0x00000002,
BATTERY_SYSTEM_BATTERY = 0x80000000
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct BATTERY_INFORMATION
{
public BATTERY_CAPABILITIES Capabilities;
public byte Technology;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] Reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] Chemistry;
public int DesignedCapacity;
public int FullChargedCapacity;
public int DefaultAlert1;
public int DefaultAlert2;
public int CriticalBias;
public int CycleCount;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct BATTERY_MANUFACTURE_DATE
{
public byte Day;
public byte Month;
public byte Year;
}
[Flags]
internal enum POWER_STATE : uint
{
BATTERY_POWER_ONLINE = 0x00000001,
BATTERY_DISCHARGING = 0x00000002,
BATTERY_CHARGING = 0x00000004,
BATTERY_CRITICAL = 0x00000008
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct BATTERY_STATUS
{
public POWER_STATE PowerState;
public uint Capacity;
public uint Voltage;
public int Rate;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct BATTERY_WAIT_STATUS
{
public uint BatteryTag;
public uint Timeout;
public uint PowerState;
public uint LowCapacity;
public uint HighCapacity;
}
}
}