-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYBatteries.cpp
More file actions
80 lines (67 loc) · 2.76 KB
/
Copy pathYBatteries.cpp
File metadata and controls
80 lines (67 loc) · 2.76 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
/*
Yuryu's Battery Information, displays detailed battery info.
Copyright 2008 Yuryu / Emma Haruka Iwao
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// YBatteries.cpp: CYBatteries クラスのインプリメンテーション
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "YbInfo.h"
#include "YBatteries.h"
#include "YBattery.h"
//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////
namespace {
constexpr DWORD MaxBatteries = 100;
}
CYBatteries::CYBatteries()
{
const HDEVINFO hRawDevInfo = ::SetupDiGetClassDevs(&GUID_DEVICE_BATTERY, nullptr, nullptr, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
if(hRawDevInfo == INVALID_HANDLE_VALUE){
return;
}
const std::unique_ptr<std::remove_pointer<HDEVINFO>::type, decltype(&::SetupDiDestroyDeviceInfoList)> hBatDevInfo(
hRawDevInfo, ::SetupDiDestroyDeviceInfoList);
// device interface indices are not guaranteed to be contiguous, so failures
// other than ERROR_NO_MORE_ITEMS don't end the enumeration; the index cap
// keeps a persistent error from looping forever
for(DWORD nIndex = 0; nIndex < MaxBatteries; nIndex++){
SP_DEVICE_INTERFACE_DATA devInterfaceData;
devInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
const BOOL bEnumDev = ::SetupDiEnumDeviceInterfaces(hBatDevInfo.get(), nullptr, &GUID_DEVICE_BATTERY, nIndex, &devInterfaceData);
if(!bEnumDev){
if(::GetLastError() == ERROR_NO_MORE_ITEMS){
break;
}
continue;
}
DWORD reqSize = 0;
::SetupDiGetDeviceInterfaceDetail(hBatDevInfo.get(), &devInterfaceData, nullptr, 0, &reqSize, nullptr);
if(::GetLastError() != ERROR_INSUFFICIENT_BUFFER){
continue;
}
CHeapPtr<SP_DEVICE_INTERFACE_DETAIL_DATA> pDevDetailData;
if(!pDevDetailData.AllocateBytes(reqSize)){
continue;
}
// cbSize must always be the fixed length of the struct.
pDevDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if(!::SetupDiGetDeviceInterfaceDetail(hBatDevInfo.get(), &devInterfaceData, pDevDetailData, reqSize, nullptr, nullptr)){
continue;
}
auto pcyBattery = std::make_unique<CYBattery>(pDevDetailData->DevicePath);
if (pcyBattery->Open()) {
m_vcpBatteries.push_back(std::move(pcyBattery));
}
}
}