-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattery.cs
More file actions
586 lines (459 loc) · 24.1 KB
/
Copy pathBattery.cs
File metadata and controls
586 lines (459 loc) · 24.1 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
// based on https://gist.github.com/ahawker/9715872
namespace BatteryMonitor
{
[Flags]
public enum PowerStates
{
PowerOnline = 0x00000001,
Discharging = 0x00000002,
Charging = 0x00000004,
Critical = 0x00000008
}
public class BatteryData
{
public BatteryData(int index,
bool isSystemPowerBattery,
string deviceName,
string manufactureName,
DateTime manufactureDate,
string chemistry,
int designedMaxCapacity,
int fullChargeCapacity,
int currentCapacity,
int voltage,
TimeSpan estimatedTime,
int dischargeRate,
int cycleCount,
int defaultAlert1,
int defaultAlert2,
int criticalBias,
PowerStates powerState,
double temperature)
{
Index = index;
IsSystemPowerBattery = isSystemPowerBattery;
DeviceName = deviceName;
ManufactureName = manufactureName;
ManufactureDate = manufactureDate;
Chemistry = chemistry;
DesignedMaxCapacity = designedMaxCapacity;
FullChargedCapacity = fullChargeCapacity;
CurrentCapacity = currentCapacity;
Voltage = voltage;
EstimatedTime = estimatedTime;
DischargeRate = dischargeRate;
CycleCount = cycleCount;
DefaultAlert1 = defaultAlert1;
DefaultAlert2 = defaultAlert2;
CriticalBias = criticalBias;
PowerState = powerState;
Temperature = temperature;
}
public int Index { get; }
public bool IsSystemPowerBattery { get; }
public string DeviceName { get; }
public string ManufactureName { get; }
public DateTime ManufactureDate { get; }
public int CurrentCapacity { get; }
public int DesignedMaxCapacity { get; }
public int FullChargedCapacity { get; }
public int Voltage { get; }
public TimeSpan EstimatedTime { get; }
public int DischargeRate { get; }
public int CycleCount { get; }
public int DefaultAlert1 { get; }
public int DefaultAlert2 { get; }
public int CriticalBias { get; }
public PowerStates PowerState { get; }
public double Temperature { get; }
public string Chemistry { get; }
}
public static class BatteryInformation
{
public static List<int> GetSystemBatteryIndexes()
{
List<int> batteryIndexes = new List<int>();
IntPtr deviceHandle = SetupDiGetClassDevs(NativeMethods.GUID_DEVCLASS_BATTERY,
NativeMethods.DEVICE_GET_CLASS_FLAGS.DIGCF_PRESENT | NativeMethods.DEVICE_GET_CLASS_FLAGS.DIGCF_DEVICEINTERFACE);
int batteryIndex = 0;
while (true)
{
var deviceInterfaceData = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
deviceInterfaceData.CbSize = Marshal.SizeOf(deviceInterfaceData);
if (!SetupDiEnumDeviceInterfaces(deviceHandle, NativeMethods.GUID_DEVCLASS_BATTERY, batteryIndex, ref deviceInterfaceData))
{
break;
}
var deviceDetailData = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA();
deviceDetailData.CbSize = (IntPtr.Size == 8) ? 8 : 4 + Marshal.SystemDefaultCharSize;
SetupDiGetDeviceInterfaceDetail(deviceHandle, ref deviceInterfaceData, ref deviceDetailData, NativeMethods.DEVICE_INTERFACE_BUFFER_SIZE);
var batteryHandle = CreateFile(deviceDetailData.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, FileMode.Open, NativeMethods.FILE_ATTRIBUTES.Normal);
uint batteryTag = batteryTag = GetBatteryTag(batteryHandle);
var batteryInformation = GetBatteryInformation(batteryHandle, batteryTag);
if ((batteryInformation.Capabilities & NativeMethods.BATTERY_CAPABILITIES.BATTERY_SYSTEM_BATTERY) != 0
&& (batteryInformation.Capabilities & NativeMethods.BATTERY_CAPABILITIES.BATTERY_IS_SHORT_TERM) == 0)
{
batteryIndexes.Add(batteryIndex);
}
NativeMethods.CloseHandle(batteryHandle);
batteryIndex++;
}
NativeMethods.SetupDiDestroyDeviceInfoList(deviceHandle);
return batteryIndexes;
}
public static List<BatteryData> GetAllSystemBatteries()
{
var batteries = new List<BatteryData>();
IntPtr deviceHandle = SetupDiGetClassDevs(NativeMethods.GUID_DEVCLASS_BATTERY,
NativeMethods.DEVICE_GET_CLASS_FLAGS.DIGCF_PRESENT | NativeMethods.DEVICE_GET_CLASS_FLAGS.DIGCF_DEVICEINTERFACE);
int batteryIndex = 0;
while (true)
{
var batteryInfo = GetOneBatteryInfo(deviceHandle, batteryIndex);
if (batteryInfo == null)
{
break;
}
if (batteryInfo.IsSystemPowerBattery)
{
batteries.Add(batteryInfo);
}
batteryIndex++;
}
NativeMethods.SetupDiDestroyDeviceInfoList(deviceHandle);
return batteries;
}
public static BatteryData GetBattery(int batteryIndex)
{
IntPtr deviceHandle = SetupDiGetClassDevs(NativeMethods.GUID_DEVCLASS_BATTERY,
NativeMethods.DEVICE_GET_CLASS_FLAGS.DIGCF_PRESENT | NativeMethods.DEVICE_GET_CLASS_FLAGS.DIGCF_DEVICEINTERFACE);
var batteryInfo = GetOneBatteryInfo(deviceHandle, batteryIndex);
NativeMethods.SetupDiDestroyDeviceInfoList(deviceHandle);
return batteryInfo;
}
private static BatteryData GetOneBatteryInfo(IntPtr deviceHandle, int batteryIndex)
{
IntPtr batteryHandle = IntPtr.Zero;
bool isSystemPowerBatterie = false;
var deviceInterfaceData = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
deviceInterfaceData.CbSize = Marshal.SizeOf(deviceInterfaceData);
if (!SetupDiEnumDeviceInterfaces(deviceHandle, NativeMethods.GUID_DEVCLASS_BATTERY, batteryIndex, ref deviceInterfaceData))
{
return null;
}
var deviceDetailData = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA();
deviceDetailData.CbSize = (IntPtr.Size == 8) ? 8 : 4 + Marshal.SystemDefaultCharSize;
SetupDiGetDeviceInterfaceDetail(deviceHandle, ref deviceInterfaceData, ref deviceDetailData, NativeMethods.DEVICE_INTERFACE_BUFFER_SIZE);
batteryHandle = CreateFile(deviceDetailData.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, FileMode.Open, NativeMethods.FILE_ATTRIBUTES.Normal);
// get the battery tag
uint batteryTag = batteryTag = GetBatteryTag(batteryHandle);
var batteryInformation = GetBatteryInformation(batteryHandle, batteryTag);
// detect none-UPS: http://msdn.microsoft.com/en-us/library/windows/desktop/bb204769%28v=vs.85%29.aspx
if ((batteryInformation.Capabilities & NativeMethods.BATTERY_CAPABILITIES.BATTERY_SYSTEM_BATTERY) != 0
&& (batteryInformation.Capabilities & NativeMethods.BATTERY_CAPABILITIES.BATTERY_IS_SHORT_TERM) == 0)
{
isSystemPowerBatterie = true;
}
var deviceName = GetBatteryDeviceName(batteryHandle, batteryTag);
var manufactureName = GetBatteryManufactureName(batteryHandle, batteryTag);
var manufactureDate = GetBatteryManufactureDate(batteryHandle, batteryTag);
var estimatedTime = GetBatteryEstimatedTime(batteryHandle, batteryTag);
var temperature = GetBatteryTemperature(batteryHandle, batteryTag);
var batterieStatus = GetBatteryStatus(batteryHandle, batteryTag);
NativeMethods.CloseHandle(batteryHandle);
return new BatteryData(
batteryIndex,
isSystemPowerBatterie,
deviceName,
manufactureName,
manufactureDate,
ConvertToString(batteryInformation.Chemistry),
batteryInformation.DesignedCapacity,
batteryInformation.FullChargedCapacity,
(int)batterieStatus.Capacity,
(int)batterieStatus.Voltage,
estimatedTime,
batterieStatus.Rate,
batteryInformation.CycleCount,
batteryInformation.DefaultAlert1,
batteryInformation.DefaultAlert2,
batteryInformation.CriticalBias,
(PowerStates)batterieStatus.PowerState,
temperature);
}
private static string ConvertToString(byte[] bytes)
{
string str = string.Empty;
foreach (byte b in bytes)
{
if (b == 0)
{
break;
}
str += (char)b;
}
return str;
}
private static uint GetBatteryTag(IntPtr batteryHandle)
{
// https://learn.microsoft.com/de-de/windows/win32/power/battery-information
uint batteryTag = 0;
DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_TAG, ref batteryTag);
return batteryTag;
}
private static string GetBatteryManufactureName(IntPtr batteryHandle, uint batteryTag)
{
// https://learn.microsoft.com/de-de/windows/win32/power/ioctl-battery-query-information
var queryInformation = new NativeMethods.BATTERY_QUERY_INFORMATION();
queryInformation.BatteryTag = batteryTag;
queryInformation.InformationLevel = NativeMethods.BATTERY_QUERY_INFORMATION_LEVEL.BatteryManufactureName;
int queryInfoSize = Marshal.SizeOf(queryInformation);
IntPtr queryInfoPointer = Marshal.AllocHGlobal(queryInfoSize);
Marshal.StructureToPtr(queryInformation, queryInfoPointer, false);
int nameSize = 500;
IntPtr namePointer = Marshal.AllocHGlobal(nameSize);
DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_INFORMATION, queryInfoPointer, queryInfoSize, namePointer, nameSize);
string name = Marshal.PtrToStringUni(namePointer);
Marshal.FreeHGlobal(queryInfoPointer);
Marshal.FreeHGlobal(namePointer);
return name;
}
private static string GetBatteryDeviceName(IntPtr batteryHandle, uint batteryTag)
{
var queryInformation = new NativeMethods.BATTERY_QUERY_INFORMATION();
queryInformation.BatteryTag = batteryTag;
queryInformation.InformationLevel = NativeMethods.BATTERY_QUERY_INFORMATION_LEVEL.BatteryDeviceName;
int queryInfoSize = Marshal.SizeOf(queryInformation);
IntPtr queryInfoPointer = Marshal.AllocHGlobal(queryInfoSize);
Marshal.StructureToPtr(queryInformation, queryInfoPointer, false);
int nameSize = 500;
IntPtr namePointer = Marshal.AllocHGlobal(nameSize);
DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_INFORMATION, queryInfoPointer, queryInfoSize, namePointer, nameSize);
string name = Marshal.PtrToStringUni(namePointer);
Marshal.FreeHGlobal(queryInfoPointer);
Marshal.FreeHGlobal(namePointer);
return name;
}
private static TimeSpan GetBatteryEstimatedTime(IntPtr batteryHandle, uint batteryTag)
{
var queryInformation = new NativeMethods.BATTERY_QUERY_INFORMATION();
queryInformation.BatteryTag = batteryTag;
queryInformation.InformationLevel = NativeMethods.BATTERY_QUERY_INFORMATION_LEVEL.BatteryEstimatedTime;
queryInformation.AtRate = 0;
int queryInfoSize = Marshal.SizeOf(queryInformation);
IntPtr queryInfoPointer = Marshal.AllocHGlobal(queryInfoSize);
Marshal.StructureToPtr(queryInformation, queryInfoPointer, false);
var timeSize = Marshal.SizeOf<uint>();
IntPtr timePointer = Marshal.AllocHGlobal(timeSize);
DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_INFORMATION, queryInfoPointer, queryInfoSize, timePointer, timeSize);
var seconds = Marshal.PtrToStructure<uint>(timePointer);
Marshal.FreeHGlobal(queryInfoPointer);
Marshal.FreeHGlobal(timePointer);
TimeSpan estimatedTime;
if (seconds == NativeMethods.BATTERY_UNKNOWN_TIME)
{
estimatedTime = TimeSpan.Zero;
}
else
{
estimatedTime = TimeSpan.FromSeconds(seconds);
}
return estimatedTime;
}
private static double GetBatteryTemperature(IntPtr batteryHandle, uint batteryTag)
{
var queryInformation = new NativeMethods.BATTERY_QUERY_INFORMATION();
queryInformation.BatteryTag = batteryTag;
queryInformation.InformationLevel = NativeMethods.BATTERY_QUERY_INFORMATION_LEVEL.BatteryTemperature;
queryInformation.AtRate = 0;
int queryInfoSize = Marshal.SizeOf(queryInformation);
IntPtr queryInfoPointer = Marshal.AllocHGlobal(queryInfoSize);
Marshal.StructureToPtr(queryInformation, queryInfoPointer, false);
var temperatureSize = Marshal.SizeOf<uint>();
IntPtr temperaturePointer = Marshal.AllocHGlobal(temperatureSize);
if (!DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_INFORMATION, queryInfoPointer, queryInfoSize, temperaturePointer, temperatureSize))
{
return double.NaN;
}
uint temperature = Marshal.PtrToStructure<uint>(temperaturePointer);
Marshal.FreeHGlobal(queryInfoPointer);
Marshal.FreeHGlobal(temperaturePointer);
return temperature / 10.0;
}
private static DateTime GetBatteryManufactureDate(IntPtr batteryHandle, uint batteryTag)
{
var queryInformation = new NativeMethods.BATTERY_QUERY_INFORMATION();
queryInformation.BatteryTag = batteryTag;
queryInformation.InformationLevel = NativeMethods.BATTERY_QUERY_INFORMATION_LEVEL.BatteryManufactureDate;
queryInformation.AtRate = 0;
int queryInfoSize = Marshal.SizeOf(queryInformation);
IntPtr queryInfoPointer = Marshal.AllocHGlobal(queryInfoSize);
Marshal.StructureToPtr(queryInformation, queryInfoPointer, false);
var dateSize = 500; //Marshal.SizeOf<Win32.BATTERY_MANUFACTURE_DATE>();
IntPtr datePointer = Marshal.AllocHGlobal(dateSize);
if (!DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_INFORMATION, queryInfoPointer, queryInfoSize, datePointer, dateSize))
{
return DateTime.MinValue;
}
var date = Marshal.PtrToStructure<NativeMethods.BATTERY_MANUFACTURE_DATE>(datePointer);
Marshal.FreeHGlobal(queryInfoPointer);
Marshal.FreeHGlobal(datePointer);
return new DateTime(date.Year, date.Month, date.Day);
}
private static NativeMethods.BATTERY_INFORMATION GetBatteryInformation(IntPtr batteryHandle, uint batteryTag)
{
var queryInformation = new NativeMethods.BATTERY_QUERY_INFORMATION();
queryInformation.BatteryTag = batteryTag;
queryInformation.InformationLevel = NativeMethods.BATTERY_QUERY_INFORMATION_LEVEL.BatteryInformation;
int queryInfoSize = Marshal.SizeOf(queryInformation);
IntPtr queryInfoPointer = Marshal.AllocHGlobal(queryInfoSize);
Marshal.StructureToPtr(queryInformation, queryInfoPointer, false);
int batteryInfoSize = Marshal.SizeOf<NativeMethods.BATTERY_INFORMATION>();
IntPtr batteryInfoPointer = Marshal.AllocHGlobal(batteryInfoSize);
DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_INFORMATION, queryInfoPointer, queryInfoSize, batteryInfoPointer, batteryInfoSize);
var batteryInformation = Marshal.PtrToStructure<NativeMethods.BATTERY_INFORMATION>(batteryInfoPointer);
Marshal.FreeHGlobal(queryInfoPointer);
Marshal.FreeHGlobal(batteryInfoPointer);
return batteryInformation;
}
private static NativeMethods.BATTERY_STATUS GetBatteryStatus(IntPtr batteryHandle, uint batteryTag)
{
var batteryWaitStatus = new NativeMethods.BATTERY_WAIT_STATUS();
batteryWaitStatus.BatteryTag = batteryTag;
int waitStatusSize = Marshal.SizeOf(batteryWaitStatus);
IntPtr batteryWaitStatusPointer = Marshal.AllocHGlobal(waitStatusSize);
Marshal.StructureToPtr(batteryWaitStatus, batteryWaitStatusPointer, false);
int batteryStatusSize = Marshal.SizeOf<NativeMethods.BATTERY_STATUS>();
IntPtr batteryStatusPointer = Marshal.AllocHGlobal(batteryStatusSize);
DeviceIoControl(batteryHandle, NativeMethods.IOCTL_BATTERY_QUERY_STATUS, batteryWaitStatusPointer, waitStatusSize, batteryStatusPointer, batteryStatusSize);
var batteryStatus = Marshal.PtrToStructure<NativeMethods.BATTERY_STATUS>(batteryStatusPointer);
Marshal.FreeHGlobal(batteryStatusPointer);
Marshal.FreeHGlobal(batteryWaitStatusPointer);
return batteryStatus;
}
private static bool DeviceIoControl(IntPtr deviceHandle, uint controlCode, ref uint output)
{
uint bytesReturned;
uint junkInput = 0;
bool retval = NativeMethods.DeviceIoControl(deviceHandle, controlCode, ref junkInput, 0, ref output, (uint)Marshal.SizeOf(output), out bytesReturned, IntPtr.Zero);
if (!retval)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != 0)
throw Marshal.GetExceptionForHR(errorCode);
else
throw new Exception("DeviceIoControl call failed but Win32 didn't catch an error.");
}
return retval;
}
private static bool DeviceIoControl(IntPtr deviceHandle, uint controlCode, IntPtr input, int inputSize, IntPtr output, int outputSize)
{
uint bytesReturned;
bool retval = NativeMethods.DeviceIoControl(deviceHandle, controlCode, input, (uint)inputSize, output, (uint)outputSize, out bytesReturned, IntPtr.Zero);
if (!retval)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode == NativeMethods.ERROR_INVALID_FUNCTION)
{
return false;
}
if (errorCode != 0)
throw Marshal.GetExceptionForHR(errorCode);
else
throw new Exception("DeviceIoControl call failed but Win32 didn't catch an error.");
}
return retval;
}
private static IntPtr SetupDiGetClassDevs(Guid guid, NativeMethods.DEVICE_GET_CLASS_FLAGS flags)
{
IntPtr handle = NativeMethods.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, flags);
if (handle == IntPtr.Zero || handle.ToInt64() == -1)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != 0)
throw Marshal.GetExceptionForHR(errorCode);
else
throw new Exception("SetupDiGetClassDev call returned a bad handle.");
}
return handle;
}
private static bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet,
Guid guid,
int memberIndex,
ref NativeMethods.SP_DEVICE_INTERFACE_DATA deviceInterfaceData)
{
bool retval = NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref guid, (uint)memberIndex, ref deviceInterfaceData);
if (!retval)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != 0)
{
if (errorCode == NativeMethods.ERROR_NO_MORE_ITEMS)
{
return false;
}
throw Marshal.GetExceptionForHR(errorCode);
}
else
throw new Exception("SetupDeviceInfoEnumerateDeviceInterfaces call failed but Win32 didn't catch an error.");
}
return retval;
}
private static bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet)
{
bool retval = NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);
if (!retval)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != 0)
throw Marshal.GetExceptionForHR(errorCode);
else
throw new Exception("SetupDiDestroyDeviceInfoList call failed but Win32 didn't catch an error.");
}
return retval;
}
private static bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet,
ref NativeMethods.SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
ref NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
int deviceInterfaceDetailSize)
{
uint reqSize;
bool retval = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
ref deviceInterfaceData,
ref deviceInterfaceDetailData,
(uint)deviceInterfaceDetailSize,
out reqSize,
IntPtr.Zero);
if (!retval)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != 0)
throw Marshal.GetExceptionForHR(errorCode);
else
throw new Exception("SetupDiGetDeviceInterfaceDetail call failed but Win32 didn't catch an error.");
}
return retval;
}
private static IntPtr CreateFile(string filename,
FileAccess access,
FileShare shareMode,
FileMode creation,
NativeMethods.FILE_ATTRIBUTES flags)
{
IntPtr handle = NativeMethods.CreateFile(filename, access, shareMode, IntPtr.Zero, creation, flags, IntPtr.Zero);
if (handle == IntPtr.Zero || handle.ToInt64() == -1)
{
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != 0)
Marshal.ThrowExceptionForHR(errorCode);
else
throw new Exception("SetupDiGetDeviceInterfaceDetail call failed but Win32 didn't catch an error.");
}
return handle;
}
}
}