使用WMI的电池FullChargeCapacity返回0

时间:2015-12-03 02:41:50

标签: c++ windows winapi wmi battery

我尝试在C ++中使用WMI。在WMI类CIM_Battery中,FullChargeCapacity值只返回0.

还有其他方法可以获得FullChargeCapacity吗?

我尝试的代码是:

IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;

while (pEnumerator)
{
    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
        &pclsObj, &uReturn);

    if (0 == uReturn)
    {
        break;
    }

    VARIANT vtProp;

    // Get the value of the Name property


    hr = pclsObj->Get(L"FullChargeCapacity", 0, &vtProp, 0, 0);
    wcout << " FullChargeCapacity  : " << vtProp.ulVal << endl;
    VariantClear(&vtProp);
    /*hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
    wcout << " Name  : " << vtProp.bstrVal << endl;
    VariantClear(&vtProp);*/

    pclsObj->Release();
}

1 个答案:

答案 0 :(得分:0)

正如您自己想的那样,使用WMI您将无法获得所需信息,您将不得不直接与电池驱动程序通信。

电池驱动器通过I²C与电池通信,并可访问这些值。幸运的是,您可以使用一些特定于电池的IOCLT代码。

您首先需要知道电池驱动程序的 deviceName 。下面的方法使用SetupApi,但您也可以从注册表HKEY_LOCAL_MACKINE\SYSTEM\CurrentControlSet\Services\CmBatt\Enum\0获取它(假设您的电池使用 CmBatt.sys 驱动程序)。我建议使用SetupApi。

下面是一个帮助函数来获取 deviceName

#include <Windows.h>
#include <SetupAPI.h>
#include <batclass.h>
#include <devguid.h>
#include <string>
#include <iostream>

#ifndef UNICODE
typedef std::string String;
#else
typedef std::wstring String;
#endif

const String GetBatteryDevicePath(const int batteryIndex)
{
    String result = TEXT("");
    HDEVINFO hDeviceInfoList = ::SetupDiGetClassDevs(&GUID_DEVCLASS_BATTERY, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (hDeviceInfoList != INVALID_HANDLE_VALUE)
    {
        SP_DEVICE_INTERFACE_DATA did = { 0 };
        did.cbSize = sizeof(did);
        if (::SetupDiEnumDeviceInterfaces(hDeviceInfoList, 0, &GUID_DEVCLASS_BATTERY, batteryIndex, &did))
        {
            DWORD dwRequiredSize = 0;
            ::SetupDiGetDeviceInterfaceDetail(hDeviceInfoList, &did, 0, 0, &dwRequiredSize, 0);
            if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::LocalAlloc(LPTR, dwRequiredSize);
                if (pdidd)
                {
                    pdidd->cbSize = sizeof(*pdidd);
                    if (::SetupDiGetDeviceInterfaceDetail(hDeviceInfoList, &did, pdidd, dwRequiredSize, &dwRequiredSize, 0))
                    {
                        result = pdidd->DevicePath;
                    }

                    ::LocalFree(pdidd);
                }
            }
        }

        ::SetupDiDestroyDeviceInfoList(hDeviceInfoList);
    }

    return result;
}

之后,您可以使用CreateFile打开电池设备的手柄:

const String devicePath = ::GetBatteryDevicePath(0);

const auto hBattery = ::CreateFile(devicePath.c_str(),
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

现在我们需要电池tag,这是驱动程序生成的电池的标识符。 F.E.如果您更换电池,驱动程序将生成一个新标签。使用此标记,我们可以查询剩余的电池信息(请参阅BATTERY_INFORMATION):

if (hBattery != INVALID_HANDLE_VALUE)
{
    DWORD dwBytesReturned, dwWait = 0;
    BATTERY_QUERY_INFORMATION bqi = { 0 };
    // Query battery tag
    if (::DeviceIoControl(hBattery,
            IOCTL_BATTERY_QUERY_TAG,
            &dwWait,
            sizeof(dwWait),
            &bqi.BatteryTag,
            sizeof(bqi.BatteryTag),
            &dwBytesReturned,
            NULL)
        && bqi.BatteryTag)
    {
        // Now we can query all other battery info
        BATTERY_INFORMATION bi = { 0 };
        if (::DeviceIoControl(hBattery,
                IOCTL_BATTERY_QUERY_INFORMATION,
                &bqi,
                sizeof(bqi),
                &bi,
                sizeof(bi),
                &dwBytesReturned,
                NULL))
        {
            std::cout << "FullChargedCapacity = " << bi.FullChargedCapacity << std::endl;
            std::cout << "DesignedCapacity = " << bi.DesignedCapacity << std::endl;
        }
    }

    ::CloseHandle(hBattery);
}

MSDN Power Management Control Codes

上列出了IOCTL代码和相关结构

<强>更新

根据您的评论中的要求,要阅读时间估算,请按照上一代码段中的bqi请求标记,然后像这样查询BatteryEstimatedTime

bqi.AtRate = 0;
bqi.InformationLevel = BatteryEstimatedTime;
ULONG lEstimatedTime;
if (::DeviceIoControl(hBattery,
        IOCTL_BATTERY_QUERY_INFORMATION,
        &bqi,
        sizeof(bqi),
        &lEstimatedTime,
        sizeof(lEstimatedTime),
        &dwBytesReturned,
        NULL))
{
    if (lEstimatedTime != BATTERY_UNKNOWN_TIME)
        std::cout << "EstimatedTime = " << lEstimatedTime / 60 << "m" << std::endl;
    else
        std::cout << "EstimatedTime = UNKNOWN" << std::endl;
}

读出实际电压和容量:

BATTERY_WAIT_STATUS bws = { 0 };
BATTERY_STATUS bs = { 0 };
bws.BatteryTag = bqi.BatteryTag;
if (::DeviceIoControl(hBattery,
    IOCTL_BATTERY_QUERY_STATUS,
    &bws,
    sizeof(bws),
    &bs,
    sizeof(bs),
    &dwBytesReturned,
    NULL))
{
    if (bs.PowerState & BATTERY_CHARGING)
        std::cout << "Battery is CHARGING" << std::endl;
    if (bs.PowerState & BATTERY_DISCHARGING)
        std::cout << "Battery is DISCHARGING" << std::endl;
    if (bs.PowerState & BATTERY_POWER_ON_LINE)
        std::cout << "Power on-line" << std::endl;

    std::cout << "Battery voltage: " << bs.Voltage << "mV" << std::endl;
    std::cout << "Battery capacity: " << bs.Capacity << "mW" << std::endl;
}

通过将bs.Capacitybi.FullChargedCapacity进行比较,您可以获得电池剩余容量的百分比指示。