-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKernel32.cs
1615 lines (1364 loc) · 51.2 KB
/
Kernel32.cs
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Copyright (C) LibreHardwareMonitor and Contributors.
// All Rights Reserved.
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
// ReSharper disable InconsistentNaming
namespace PowerTray;
public class Kernel32
{
public const int ERROR_SERVICE_ALREADY_RUNNING = unchecked((int)0x80070420);
public const int ERROR_SERVICE_EXISTS = unchecked((int)0x80070431);
internal const uint BATTERY_UNKNOWN_TIME = 0xFFFFFFFF;
internal const string IntelNVMeMiniPortSignature1 = "NvmeMini";
internal const string IntelNVMeMiniPortSignature2 = "IntelNvm";
internal const uint LPTR = 0x0000 | 0x0040;
internal const int MAX_DRIVE_ATTRIBUTES = 512;
internal const uint NVME_PASS_THROUGH_SRB_IO_CODE = 0xe0002000;
internal const byte SMART_LBA_HI = 0xC2;
internal const byte SMART_LBA_HI_EXCEEDED = 0x2C;
internal const byte SMART_LBA_MID = 0x4F;
internal const byte SMART_LBA_MID_EXCEEDED = 0xF4;
private const string DllName = "kernel32.dll";
[Flags]
public enum NVME_CRITICAL_WARNING
{
None = 0x00,
/// <summary>
/// If set to 1, then the available spare space has fallen below the threshold.
/// </summary>
AvailableSpaceLow = 0x01,
/// <summary>
/// If set to 1, then a temperature is above an over temperature threshold or below an under temperature threshold.
/// </summary>
TemperatureThreshold = 0x02,
/// <summary>
/// If set to 1, then the device reliability has been degraded due to significant media related errors or any internal error that degrades device reliability.
/// </summary>
ReliabilityDegraded = 0x04,
/// <summary>
/// If set to 1, then the media has been placed in read only mode
/// </summary>
ReadOnly = 0x08,
/// <summary>
/// If set to 1, then the volatile memory backup device has failed. This field is only valid if the controller has a volatile memory backup solution.
/// </summary>
VolatileMemoryBackupDeviceFailed = 0x10
}
/// <summary>
/// Create a instance from a struct with zero initialized memory arrays
/// no need to init every inner array with the correct sizes
/// </summary>
/// <typeparam name="T">type of struct that is needed</typeparam>
/// <returns></returns>
internal static T CreateStruct<T>()
{
int size = Marshal.SizeOf<T>();
IntPtr ptr = Marshal.AllocHGlobal(size);
RtlZeroMemory(ptr, size);
T result = Marshal.PtrToStructure<T>(ptr);
Marshal.FreeHGlobal(ptr);
return result;
}
internal static SafeFileHandle OpenDevice(string devicePath)
{
SafeFileHandle hDevice = CreateFile(devicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
if (hDevice.IsInvalid || hDevice.IsClosed)
hDevice = null;
return hDevice;
}
[DllImport(DllName, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern SafeFileHandle CreateFile
(
[MarshalAs(UnmanagedType.LPTStr)] string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
DFP dwIoControlCode,
ref SENDCMDINPARAMS lpInBuffer,
int nInBufferSize,
out ATTRIBUTECMDOUTPARAMS lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
DFP dwIoControlCode,
ref SENDCMDINPARAMS lpInBuffer,
int nInBufferSize,
out THRESHOLDCMDOUTPARAMS lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
DFP dwIoControlCode,
ref SENDCMDINPARAMS lpInBuffer,
int nInBufferSize,
out SENDCMDOUTPARAMS lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
DFP dwIoControlCode,
ref SENDCMDINPARAMS lpInBuffer,
int nInBufferSize,
out IDENTIFYCMDOUTPARAMS lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
DFP dwIoControlCode,
ref SENDCMDINPARAMS lpInBuffer,
int nInBufferSize,
out STATUSCMDOUTPARAMS lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
IOCTL dwIoControlCode,
ref STORAGE_PROPERTY_QUERY lpInBuffer,
int nInBufferSize,
out STORAGE_DEVICE_DESCRIPTOR_HEADER lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
IOCTL dwIoControlCode,
ref STORAGE_PROPERTY_QUERY lpInBuffer,
int nInBufferSize,
IntPtr lpOutBuffer,
uint nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
IOCTL dwIoControlCode,
IntPtr lpInBuffer,
int nInBufferSize,
IntPtr lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeHandle hDevice,
IOCTL dwIoControlCode,
IntPtr lpInBuffer,
int nInBufferSize,
out DISK_PERFORMANCE lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeFileHandle hDevice,
IOCTL dwIoControlCode,
ref BATTERY_QUERY_INFORMATION lpInBuffer,
int nInBufferSize,
ref BATTERY_INFORMATION lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeFileHandle hDevice,
IOCTL dwIoControlCode,
ref uint lpInBuffer,
int nInBufferSize,
ref uint lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeFileHandle hDevice,
IOCTL dwIoControlCode,
ref BATTERY_WAIT_STATUS lpInBuffer,
int nInBufferSize,
ref BATTERY_STATUS lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeFileHandle hDevice,
IOCTL dwIoControlCode,
ref BATTERY_QUERY_INFORMATION lpInBuffer,
int nInBufferSize,
IntPtr lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl
(
SafeFileHandle hDevice,
IOCTL dwIoControlCode,
ref BATTERY_QUERY_INFORMATION lpInBuffer,
int nInBufferSize,
ref uint lpOutBuffer,
int nOutBufferSize,
out uint lpBytesReturned,
IntPtr lpOverlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr LocalAlloc(uint uFlags, ulong uBytes);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr LocalFree(IntPtr hMem);
[DllImport(DllName, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern void RtlZeroMemory(IntPtr Destination, int Length);
[DllImport(DllName, SetLastError = false)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern void RtlCopyMemory(IntPtr Destination, IntPtr Source, uint Length);
[DllImport(DllName, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr LoadLibrary(string lpFileName);
[DllImport(DllName, ExactSpelling = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr GetProcAddress(IntPtr module, string methodName);
[DllImport(DllName)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool FreeLibrary(IntPtr module);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr GetCurrentThread();
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern ushort GetActiveProcessorGroupCount();
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool SetThreadGroupAffinity(IntPtr thread, ref GROUP_AFFINITY groupAffinity, out GROUP_AFFINITY previousGroupAffinity);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, MEM flAllocationType, PAGE flProtect);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool VirtualFree(IntPtr lpAddress, UIntPtr dwSize, MEM dwFreeType);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern bool DeviceIoControl
(
SafeFileHandle device,
IOControlCode ioControlCode,
[MarshalAs(UnmanagedType.AsAny)][In] object inBuffer,
uint inBufferSize,
[MarshalAs(UnmanagedType.AsAny)][Out] object outBuffer,
uint nOutBufferSize,
out uint bytesReturned,
IntPtr overlapped);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern IntPtr CreateFile
(
string lpFileName,
uint dwDesiredAccess,
FileShare dwShareMode,
IntPtr lpSecurityAttributes,
FileMode dwCreationDisposition,
FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern int EnumSystemFirmwareTables(Provider firmwareTableProviderSignature, IntPtr firmwareTableBuffer, int bufferSize);
[DllImport(DllName, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
internal static extern int GetSystemFirmwareTable(Provider firmwareTableProviderSignature, int firmwareTableID, IntPtr firmwareTableBuffer, int bufferSize);
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct GROUP_AFFINITY
{
public UIntPtr Mask;
[MarshalAs(UnmanagedType.U2)]
public ushort Group;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.U2)]
public ushort[] Reserved;
}
internal enum DFP : uint
{
DFP_GET_VERSION = 0x00074080,
DFP_SEND_DRIVE_COMMAND = 0x0007c084,
DFP_RECEIVE_DRIVE_DATA = 0x0007c088
}
internal enum IOCTL : uint
{
IOCTL_SCSI_PASS_THROUGH = 0x04d004,
IOCTL_SCSI_MINIPORT = 0x04d008,
IOCTL_SCSI_PASS_THROUGH_DIRECT = 0x04d014,
IOCTL_SCSI_GET_ADDRESS = 0x41018,
IOCTL_DISK_PERFORMANCE = 0x70020,
IOCTL_STORAGE_QUERY_PROPERTY = 0x2D1400,
IOCTL_BATTERY_QUERY_TAG = 0x294040,
IOCTL_BATTERY_QUERY_INFORMATION = 0x294044,
IOCTL_BATTERY_QUERY_STATUS = 0x29404C
}
[Flags]
internal enum BatteryCapabilities : uint
{
BATTERY_CAPACITY_RELATIVE = 0x40000000,
BATTERY_IS_SHORT_TERM = 0x20000000,
BATTERY_SET_CHARGE_SUPPORTED = 0x00000001,
BATTERY_SET_DISCHARGE_SUPPORTED = 0x00000002,
BATTERY_SYSTEM_BATTERY = 0x80000000
}
internal enum BATTERY_QUERY_INFORMATION_LEVEL
{
BatteryInformation,
BatteryGranularityInformation,
BatteryTemperature,
BatteryEstimatedTime,
BatteryDeviceName,
BatteryManufactureDate,
BatteryManufactureName,
BatteryUniqueID,
BatterySerialNumber
}
[StructLayout(LayoutKind.Sequential)]
internal struct BATTERY_QUERY_INFORMATION
{
public uint BatteryTag;
public BATTERY_QUERY_INFORMATION_LEVEL InformationLevel;
public uint AtRate;
}
[StructLayout(LayoutKind.Sequential)]
internal struct BATTERY_INFORMATION
{
public BatteryCapabilities Capabilities;
public byte Technology;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] Reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public char[] Chemistry;
public uint DesignedCapacity;
public uint FullChargedCapacity;
public uint DefaultAlert1;
public uint DefaultAlert2;
public uint CriticalBias;
public uint CycleCount;
}
[StructLayout(LayoutKind.Sequential)]
internal struct BATTERY_WAIT_STATUS
{
public uint BatteryTag;
public uint Timeout;
public uint PowerState;
public uint LowCapacity;
public uint HighCapacity;
}
[StructLayout(LayoutKind.Sequential)]
internal struct BATTERY_STATUS
{
public uint PowerState;
public uint Capacity;
public uint Voltage;
public int Rate;
}
[Flags]
internal enum NVME_DIRECTION : uint
{
NVME_FROM_HOST_TO_DEV = 1,
NVME_FROM_DEV_TO_HOST = 2,
NVME_BI_DIRECTION = NVME_FROM_DEV_TO_HOST | NVME_FROM_HOST_TO_DEV
}
internal enum NVME_LOG_PAGES
{
NVME_LOG_PAGE_ERROR_INFO = 0x01,
NVME_LOG_PAGE_HEALTH_INFO = 0x02,
NVME_LOG_PAGE_FIRMWARE_SLOT_INFO = 0x03,
NVME_LOG_PAGE_CHANGED_NAMESPACE_LIST = 0x04,
NVME_LOG_PAGE_COMMAND_EFFECTS = 0x05,
NVME_LOG_PAGE_DEVICE_SELF_TEST = 0x06,
NVME_LOG_PAGE_TELEMETRY_HOST_INITIATED = 0x07,
NVME_LOG_PAGE_TELEMETRY_CTLR_INITIATED = 0x08,
NVME_LOG_PAGE_RESERVATION_NOTIFICATION = 0x80,
NVME_LOG_PAGE_SANITIZE_STATUS = 0x81
}
internal enum ATA_COMMAND : byte
{
/// <summary>
/// SMART data requested.
/// </summary>
ATA_SMART = 0xB0,
/// <summary>
/// Identify data is requested.
/// </summary>
ATA_IDENTIFY_DEVICE = 0xEC
}
internal enum SCSI_IOCTL_DATA
{
SCSI_IOCTL_DATA_OUT = 0,
SCSI_IOCTL_DATA_IN = 1,
SCSI_IOCTL_DATA_UNSPECIFIED = 2
}
internal enum SMART_FEATURES : byte
{
/// <summary>
/// Read SMART data.
/// </summary>
SMART_READ_DATA = 0xD0,
/// <summary>
/// Read SMART thresholds.
/// obsolete
/// </summary>
READ_THRESHOLDS = 0xD1,
/// <summary>
/// Autosave SMART data.
/// </summary>
ENABLE_DISABLE_AUTOSAVE = 0xD2,
/// <summary>
/// Save SMART attributes.
/// </summary>
SAVE_ATTRIBUTE_VALUES = 0xD3,
/// <summary>
/// Set SMART to offline immediately.
/// </summary>
EXECUTE_OFFLINE_DIAGS = 0xD4,
/// <summary>
/// Read SMART log.
/// </summary>
SMART_READ_LOG = 0xD5,
/// <summary>
/// Write SMART log.
/// </summary>
SMART_WRITE_LOG = 0xD6,
/// <summary>
/// Write SMART thresholds.
/// obsolete
/// </summary>
WRITE_THRESHOLDS = 0xD7,
/// <summary>
/// Enable SMART.
/// </summary>
ENABLE_SMART = 0xD8,
/// <summary>
/// Disable SMART.
/// </summary>
DISABLE_SMART = 0xD9,
/// <summary>
/// Get SMART status.
/// </summary>
RETURN_SMART_STATUS = 0xDA,
/// <summary>
/// Set SMART to offline automatically.
/// </summary>
ENABLE_DISABLE_AUTO_OFFLINE = 0xDB /* obsolete */
}
internal enum STORAGE_BUS_TYPE
{
BusTypeUnknown = 0x00,
BusTypeScsi,
BusTypeAtapi,
BusTypeAta,
BusType1394,
BusTypeSsa,
BusTypeFibre,
BusTypeUsb,
BusTypeRAID,
BusTypeiScsi,
BusTypeSas,
BusTypeSata,
BusTypeSd,
BusTypeMmc,
BusTypeVirtual,
BusTypeFileBackedVirtual,
BusTypeSpaces,
BusTypeNvme,
BusTypeSCM,
BusTypeMax,
BusTypeMaxReserved = 0x7F
}
internal enum STORAGE_PROPERTY_ID
{
StorageDeviceProperty = 0,
StorageAdapterProperty,
StorageDeviceIdProperty,
StorageDeviceUniqueIdProperty,
StorageDeviceWriteCacheProperty,
StorageMiniportProperty,
StorageAccessAlignmentProperty,
StorageDeviceSeekPenaltyProperty,
StorageDeviceTrimProperty,
StorageDeviceWriteAggregationProperty,
StorageDeviceDeviceTelemetryProperty,
StorageDeviceLBProvisioningProperty,
StorageDevicePowerProperty,
StorageDeviceCopyOffloadProperty,
StorageDeviceResiliencyProperty,
StorageDeviceMediumProductType,
StorageAdapterRpmbProperty,
StorageDeviceIoCapabilityProperty = 48,
StorageAdapterProtocolSpecificProperty,
StorageDeviceProtocolSpecificProperty,
StorageAdapterTemperatureProperty,
StorageDeviceTemperatureProperty,
StorageAdapterPhysicalTopologyProperty,
StorageDevicePhysicalTopologyProperty,
StorageDeviceAttributesProperty,
StorageDeviceManagementStatus,
StorageAdapterSerialNumberProperty,
StorageDeviceLocationProperty
}
internal enum STORAGE_PROTOCOL_NVME_DATA_TYPE
{
NVMeDataTypeUnknown = 0,
NVMeDataTypeIdentify,
NVMeDataTypeLogPage,
NVMeDataTypeFeature
}
internal enum STORAGE_PROTOCOL_NVME_PROTOCOL_DATA_REQUEST_VALUE
{
NVMeIdentifyCnsSpecificNamespace = 0,
NVMeIdentifyCnsController = 1,
NVMeIdentifyCnsActiveNamespaces = 2
}
internal enum STORAGE_PROTOCOL_TYPE
{
ProtocolTypeUnknown = 0x00,
ProtocolTypeScsi,
ProtocolTypeAta,
ProtocolTypeNvme,
ProtocolTypeSd,
ProtocolTypeProprietary = 0x7E,
ProtocolTypeMaxReserved = 0x7F
}
internal enum STORAGE_QUERY_TYPE
{
PropertyStandardQuery = 0,
PropertyExistsQuery,
PropertyMaskQuery,
PropertyQueryMaxDefined
}
[StructLayout(LayoutKind.Sequential)]
internal struct MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SMART_ATTRIBUTE
{
public byte Id;
public short Flags;
public byte CurrentValue;
public byte WorstValue;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] RawValue;
public byte Reserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SMART_THRESHOLD
{
public byte Id;
public byte Threshold;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public byte[] Reserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct SENDCMDINPARAMS
{
public uint cBufferSize;
public IDEREGS irDriveRegs;
public byte bDriveNumber;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] bReserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public uint[] dwReserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte[] bBuffer;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct IDEREGS
{
public SMART_FEATURES bFeaturesReg;
public byte bSectorCountReg;
public byte bSectorNumberReg;
public byte bCylLowReg;
public byte bCylHighReg;
public byte bDriveHeadReg;
public ATA_COMMAND bCommandReg;
public byte bReserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct DRIVERSTATUS
{
public byte bDriverError;
public byte bIDEError;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public byte[] Reserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct SENDCMDOUTPARAMS
{
public uint cBufferSize;
public DRIVERSTATUS DriverStatus;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte[] bBuffer;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct ATTRIBUTECMDOUTPARAMS
{
public uint cBufferSize;
public DRIVERSTATUS DriverStatus;
public byte Version;
public byte Reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DRIVE_ATTRIBUTES)]
public SMART_ATTRIBUTE[] Attributes;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct THRESHOLDCMDOUTPARAMS
{
public uint cBufferSize;
public DRIVERSTATUS DriverStatus;
public byte Version;
public byte Reserved;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_DRIVE_ATTRIBUTES)]
public SMART_THRESHOLD[] Thresholds;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct STATUSCMDOUTPARAMS
{
public uint cBufferSize;
public DRIVERSTATUS DriverStatus;
public IDEREGS irDriveRegs;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct IDENTIFY_DATA
{
public ushort GeneralConfiguration;
public ushort NumberOfCylinders;
public ushort Reserved1;
public ushort NumberOfHeads;
public ushort UnformattedBytesPerTrack;
public ushort UnformattedBytesPerSector;
public ushort SectorsPerTrack;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public ushort[] VendorUnique;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public byte[] SerialNumber;
public ushort BufferType;
public ushort BufferSectorSize;
public ushort NumberOfEccBytes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] FirmwareRevision;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
public byte[] ModelNumber;
public byte MaximumBlockTransfer;
public byte VendorUnique2;
public ushort DoubleWordIo;
public ushort Capabilities;
public ushort Reserved2;
public byte VendorUnique3;
public byte PioCycleTimingMode;
public byte VendorUnique4;
public byte DmaCycleTimingMode;
public ushort TranslationFieldsValid;
public ushort NumberOfCurrentCylinders;
public ushort NumberOfCurrentHeads;
public ushort CurrentSectorsPerTrack;
public uint CurrentSectorCapacity;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 197)]
public ushort[] Reserved3;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct IDENTIFYCMDOUTPARAMS
{
public uint cBufferSize;
public DRIVERSTATUS DriverStatus;
public IDENTIFY_DATA Identify;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STORAGE_PROPERTY_QUERY
{
public STORAGE_PROPERTY_ID PropertyId;
public STORAGE_QUERY_TYPE QueryType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public byte[] AdditionalParameters;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STORAGE_DEVICE_DESCRIPTOR_HEADER
{
public uint Version;
public uint Size;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STORAGE_DEVICE_DESCRIPTOR
{
public uint Version;
public uint Size;
public byte DeviceType;
public byte DeviceTypeModifier;
[MarshalAs(UnmanagedType.U1)]
public bool RemovableMedia;
[MarshalAs(UnmanagedType.U1)]
public bool CommandQueueing;
public uint VendorIdOffset;
public uint ProductIdOffset;
public uint ProductRevisionOffset;
public uint SerialNumberOffset;
public STORAGE_BUS_TYPE BusType;
public uint RawPropertiesLength;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DISK_PERFORMANCE
{
/// <nodoc />
public ulong BytesRead;
/// <nodoc />
public ulong BytesWritten;
/// <nodoc />
public ulong ReadTime;
/// <nodoc />
public ulong WriteTime;
/// <nodoc />
public ulong IdleTime;
/// <nodoc />
public uint ReadCount;
/// <nodoc />
public uint WriteCount;
/// <nodoc />
public uint QueueDepth;
/// <nodoc />
public uint SplitCount;
/// <nodoc />
public ulong QueryTime;
/// <nodoc />
public int StorageDeviceNumber;
/// <nodoc />
public long StorageManagerName0;
/// <nodoc />
public long StorageManagerName1;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SRB_IO_CONTROL
{
public uint HeaderLenght;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] Signature;
public uint Timeout;
public uint ControlCode;
public uint ReturnCode;
public uint Length;
}
[StructLayout(LayoutKind.Sequential)]
internal struct NVME_PASS_THROUGH_IOCTL
{
public SRB_IO_CONTROL srb;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public uint[] VendorSpecific;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public uint[] NVMeCmd;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public uint[] CplEntry;
public NVME_DIRECTION Direction;
public uint QueueId;
public uint DataBufferLen;
public uint MetaDataLen;
public uint ReturnBufferLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4096)]
public byte[] DataBuffer;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SCSI_PASS_THROUGH
{
[MarshalAs(UnmanagedType.U2)]
public ushort Length;
public byte ScsiStatus;
public byte PathId;
public byte TargetId;
public byte Lun;
public byte CdbLength;
public byte SenseInfoLength;
public byte DataIn;
public uint DataTransferLength;
public uint TimeOutValue;
public IntPtr DataBufferOffset;
public uint SenseInfoOffset;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] Cdb;