当前位置: 首页 > 工具软件 > Juice-battery > 使用案例 >

Display the battery information

魏明亮
2023-12-01

 Use this code to get Battery State

     dwResult = 1  AC State

     dwResult = 3  DC State          

  1. DWORD GetBatteryState()
  2.  {
  3. #define GBS_HASBATTERY 0x1
  4. #define GBS_ONBATTERY  0x2
  5.   // Returned value includes GBS_HASBATTERY if the system has a 
  6.   // non-UPS battery, and GBS_ONBATTERY if the system is running on 
  7.   // a battery.
  8.   //
  9.   // dwResult & GBS_ONBATTERY means AC power has not been found.
  10.   // dwResult & GBS_HASBATTERY means a non-UPS battery is being used.
  11.   DWORD dwResult = GBS_ONBATTERY;
  12.   // IOCTL_BATTERY_QUERY_INFORMATION,
  13.   // enumerate the batteries and ask each one for information.
  14.   HDEVINFO hdev =
  15.             SetupDiGetClassDevs(&GUID_DEVCLASS_BATTERY, 
  16.                                 0, 
  17.                                 0, 
  18.                                 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  19.   if (INVALID_HANDLE_VALUE != hdev)
  20.    {
  21.     // Limit search to 100 batteries max
  22.     for (int idev = 0; idev < 100; idev++)
  23.      {
  24.       SP_DEVICE_INTERFACE_DATA did = {0};
  25.       did.cbSize = sizeof(did);
  26.       if (SetupDiEnumDeviceInterfaces(hdev,
  27.                                       0,
  28.                                       &GUID_DEVCLASS_BATTERY,
  29.                                       idev,
  30.                                       &did))
  31.        {
  32.         DWORD cbRequired = 0;
  33.         SetupDiGetDeviceInterfaceDetail(hdev,
  34.                                         &did,
  35.                                         0,
  36.                                         0,
  37.                                         &cbRequired,
  38.                                         0);
  39.         if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
  40.          {
  41.           PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd =
  42.             (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR,
  43.                                                          cbRequired);
  44.           if (pdidd)
  45.            {
  46.             pdidd->cbSize = sizeof(*pdidd);
  47.             if (SetupDiGetDeviceInterfaceDetail(hdev,
  48.                                                 &did,
  49.                                                 pdidd,
  50.                                                 cbRequired,
  51.                                                 &cbRequired,
  52.                                                 0))
  53.              {
  54.               // Enumerated a battery.  Ask it for information.
  55.               HANDLE hBattery = 
  56.                       CreateFile(pdidd->DevicePath,
  57.                                  GENERIC_READ | GENERIC_WRITE,
  58.                                  FILE_SHARE_READ | FILE_SHARE_WRITE,
  59.                                  NULL,
  60.                                  OPEN_EXISTING,
  61.                                  FILE_ATTRIBUTE_NORMAL,
  62.                                  NULL);
  63.               if (INVALID_HANDLE_VALUE != hBattery)
  64.                {
  65.                 // Ask the battery for its tag.
  66.                 BATTERY_QUERY_INFORMATION bqi = {0};
  67.                 DWORD dwWait = 0;
  68.                 DWORD dwOut;
  69.                 if (DeviceIoControl(hBattery,
  70.                                     IOCTL_BATTERY_QUERY_TAG,
  71.                                     &dwWait,
  72.                                     sizeof(dwWait),
  73.                                     &bqi.BatteryTag,
  74.                                     sizeof(bqi.BatteryTag),
  75.                                     &dwOut,
  76.                                     NULL)
  77.                     && bqi.BatteryTag)
  78.                  {
  79.                   // With the tag, you can query the battery info.
  80.                   BATTERY_INFORMATION bi = {0};
  81.                   bqi.InformationLevel = BatteryInformation;
  82.                   if (DeviceIoControl(hBattery,
  83.                                       IOCTL_BATTERY_QUERY_INFORMATION,
  84.                                       &bqi,
  85.                                       sizeof(bqi),
  86.                                       &bi,
  87.                                       sizeof(bi),
  88.                                       &dwOut,
  89.                                       NULL))
  90.                    {
  91.                     // Only non-UPS system batteries count
  92.                     if (bi.Capabilities & BATTERY_SYSTEM_BATTERY)
  93.                      {
  94.                       if (!(bi.Capabilities & BATTERY_IS_SHORT_TERM))
  95.                        {
  96.                         dwResult |= GBS_HASBATTERY;
  97.                        }
  98.                       // Query the battery status.
  99.                       BATTERY_WAIT_STATUS bws = {0};
  100.                       bws.BatteryTag = bqi.BatteryTag;
  101.                       BATTERY_STATUS bs;
  102.                       if (DeviceIoControl(hBattery,
  103.                                           IOCTL_BATTERY_QUERY_STATUS,
  104.                                           &bws,
  105.                                           sizeof(bws),
  106.                                           &bs,
  107.                                           sizeof(bs),
  108.                                           &dwOut,
  109.                                           NULL))
  110.                        {
  111.                         if (bs.PowerState & BATTERY_POWER_ON_LINE)
  112.                          {
  113.                           dwResult &= ~GBS_ONBATTERY;
  114.                          }
  115.                        }
  116.                      }
  117.                    }
  118.                  }
  119.                 CloseHandle(hBattery);
  120.                }
  121.              }
  122.             LocalFree(pdidd);
  123.            }
  124.          }
  125.        }
  126.         else  if (ERROR_NO_MORE_ITEMS == GetLastError())
  127.          {
  128.           break;  // Enumeration failed - perhaps there are no more items
  129.          }
  130.      }
  131.     SetupDiDestroyDeviceInfoList(hdev);
  132.    }
  133.   //  Final cleanup:  If no battery is found, then presume that 
  134.   //  AC power is being used.
  135.   if (!(dwResult & GBS_HASBATTERY))
  136.     dwResult &= ~GBS_ONBATTERY;
  137.   return dwResult;
  138.  }

    

 类似资料:

相关阅读

相关文章

相关问答