c++中利用windows API获取电池电量和网络
电池电量
LPSYSTEM_POWER_STATUS power = new _SYSTEM_POWER_STATUS();
int ret = GetSystemPowerStatus(power);
int percent = (int)power->BatteryLifePercent;
delete power;
if (ret == 0) //表示获取失败
return "0%";
std::stringstream str;
str << percent << "%";
return str.str();
网络强度
DWORD dwError = ERROR_SUCCESS;
DWORD dwNegotiatedVersion;
HANDLE hClientHandle = NULL;
dwError = WlanOpenHandle(WLAN_API_VERSION_1_0, NULL, &dwNegotiatedVersion, &hClientHandle);
if (dwError != ERROR_SUCCESS)
return 0;
PWLAN_INTERFACE_INFO_LIST pInterfaceList = NULL;
dwError = WlanEnumInterfaces(hClientHandle, NULL, &pInterfaceList);
if (dwError != ERROR_SUCCESS)
{
WlanFreeMemory(pInterfaceList);
return 0;
}
GUID &guid = pInterfaceList->InterfaceInfo[0].InterfaceGuid;
PWLAN_AVAILABLE_NETWORK_LIST pWLAN_AVAILABLE_NETWORK_LIST = NULL;
dwError = WlanGetAvailableNetworkList(hClientHandle, &guid,
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES,
NULL, &pWLAN_AVAILABLE_NETWORK_LIST);
if (dwError != ERROR_SUCCESS)
{
WlanFreeMemory(pInterfaceList);
WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST);
return 0;
}
if (pWLAN_AVAILABLE_NETWORK_LIST->dwNumberOfItems > 0)
{
wchar_t sz[256] = { 0 };
WLAN_AVAILABLE_NETWORK wlanAN = pWLAN_AVAILABLE_NETWORK_LIST[0].Network[0];
std::stringstream str;
str<< (int)wlanAN.wlanSignalQuality;
return str.str(); //信号质量,越大越好
}
else
return "0";
WlanFreeMemory(pInterfaceList);
WlanFreeMemory(pWLAN_AVAILABLE_NETWORK_LIST);
做个记录,防止以遗忘