Get battery device name

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1208 Views - Last Post: 10 February 2013 - 03:17 PM Rate Topic: -----

#1 popgalop  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 65
  • Joined: 08-November 12

Get battery device name

Posted 07 February 2013 - 06:33 PM

I have been using windows api to get battery stats and i have figured out how to get battery data like chemistry using battery information but i cant figure out how to get stuff like manufacturer specs. Here is my current code
#define INITGUID
#include <windows.h>
#include<batclass.h>
#include<setupapi.h>
#define GBS_HASBATTERY 0x1
#define GBS_ONBATTERY  0x2
#include<iostream>

using namespace std;
                         					 
int main()
{
	DWORD dwResult = GBS_ONBATTERY;
    HDEVINFO hdev = SetupDiGetClassDevs(&GUID_DEVICE_BATTERY, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    
    for (int idev = 0; idev < 100; idev++)
     {
      SP_DEVICE_INTERFACE_DATA did = {0};
      did.cbSize = sizeof(did);

      if (SetupDiEnumDeviceInterfaces(hdev, 0, &GUID_DEVICE_BATTERY, idev, &did))
       {
        DWORD cbRequired = 0;
        SetupDiGetDeviceInterfaceDetail(hdev, &did, 0, 0, &cbRequired, 0);
       
	    if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
         {
          PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR,cbRequired);
          
		  if (pdidd)
           {
            pdidd->cbSize = sizeof(*pdidd);
            
			if (SetupDiGetDeviceInterfaceDetail(hdev,&did,pdidd,cbRequired,&cbRequired,0))
             {
              HANDLE hBattery = CreateFile(pdidd->DevicePath,GENERIC_READ | GENERIC_WRITE,
              FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
              
			  if (INVALID_HANDLE_VALUE != hBattery)
               {
                // Ask the battery for its tag.
                BATTERY_QUERY_INFORMATION bqi = {0};

                DWORD dwWait = 0;
                DWORD dwOut;

                if (DeviceIoControl(hBattery,
                                    IOCTL_BATTERY_QUERY_TAG,
                                    &dwWait,
                                    sizeof(dwWait),
                                    &bqi.BatteryTag,
                                    sizeof(bqi.BatteryTag),
                                    &dwOut,
                                    NULL)
                    && bqi.BatteryTag)
                 {
                  BATTERY_INFORMATION bi = {0};
                  bqi.InformationLevel = BatteryInformation;

                  if (DeviceIoControl(hBattery,
                                      IOCTL_BATTERY_QUERY_INFORMATION,
                                      &bqi,
                                      sizeof(bqi),
                                      &bi,
                                      sizeof(bi),
                                      &dwOut,
                                      NULL))
                   {

                    if (bi.Capabilities & BATTERY_SYSTEM_BATTERY)
                     {
                      if (!(bi.Capabilities & BATTERY_IS_SHORT_TERM))
                       {
						
                       }

                      BATTERY_WAIT_STATUS bws = {0};
                      bws.BatteryTag = bqi.BatteryTag;

                      BATTERY_STATUS bs;
                      if (DeviceIoControl(hBattery,
                                          IOCTL_BATTERY_QUERY_STATUS,
                                          &bws,
                                          sizeof(bws),
                                          &bs,
                                          sizeof(bs),
                                          &dwOut,
                                          NULL))
                       {
                        if (bs.PowerState & BATTERY_POWER_ON_LINE)
                         {
                          dwResult &= ~GBS_ONBATTERY;
                         }
                       }
                     }
                   }
                 }
                CloseHandle(hBattery);
               }
             }
            LocalFree(pdidd);
           }
         }
       }
        else  if (ERROR_NO_MORE_ITEMS == GetLastError())
         {
          break;  // Enumeration failed - perhaps we're out of items
         }
     }
    SetupDiDestroyDeviceInfoList(hdev);
   }


If anyone could give me one or two lines of code that would print that kind of data i would be very happy.

I found IOCTL_BATTERY_QUERY_INFORMATION to get the information i need but i cant figure out how to access that information from my current program.

Is This A Good Question/Topic? 0
  • +

Replies To: Get battery device name

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Get battery device name

Posted 07 February 2013 - 07:01 PM

What do you mean specs?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Get battery device name

Posted 07 February 2013 - 07:04 PM

Is that really code you wrote? It looks amazingly like the sample code from MSDN including the comments:
http://msdn.microsof...9(v=vs.85).aspx
Was This Post Helpful? 0
  • +
  • -

#4 popgalop  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 65
  • Joined: 08-November 12

Re: Get battery device name

Posted 07 February 2013 - 07:06 PM

by specs i mean information listed here http://msdn.microsof...v=vs.85%29.aspx

View PostSkydiver, on 07 February 2013 - 07:04 PM, said:

Is that really code you wrote? It looks amazingly like the sample code from MSDN including the comments:
http://msdn.microsof...9(v=vs.85).aspx

yeah its mostly from the microsoft example

This post has been edited by popgalop: 07 February 2013 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Get battery device name

Posted 07 February 2013 - 07:13 PM

and what information level?

You already get

058	                  bqi.InformationLevel = BatteryInformation;


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Get battery device name

Posted 07 February 2013 - 07:14 PM

If you actually read the documentation, and the sample code, you can see the critical lines are on line 57-67. In the sample code, it is asking for BatteryInformation and having a structure filled in. If you read the MSDN documentation that you linked to, you can also pass in BatteryManufactureDate to get a date structure filled, or BatteryManufactureName to get a buffer filled with a string.
Was This Post Helpful? 1
  • +
  • -

#7 popgalop  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 65
  • Joined: 08-November 12

Re: Get battery device name

Posted 08 February 2013 - 02:57 PM

I am trying to use this code, but it is not working, what am i doing wrong
#define INITGUID
#include <windows.h>
#include<batclass.h>
#include<setupapi.h>
#define GBS_HASBATTERY 0x1
#define GBS_ONBATTERY  0x2
#include<iostream>

using namespace std;

bool Relative[100];
bool SystemBattery[100];
bool ShortTerm[100];
int Technology[100];
string Chemistry[100];		 
int DesignedCapacity[100];
int FullChargedCapacity[100];
int DefaultAlert1[100];
int DefaultAlert2[100];
int CriticalBias[100];
int CycleCount[100];

int main()
{
	DWORD dwResult = GBS_ONBATTERY;
    HDEVINFO hdev = SetupDiGetClassDevs(&GUID_DEVICE_BATTERY, 0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    
    for (int idev = 0; idev < 100; idev++)
     {
      SP_DEVICE_INTERFACE_DATA did = {0};
      did.cbSize = sizeof(did);

      if (SetupDiEnumDeviceInterfaces(hdev, 0, &GUID_DEVICE_BATTERY, idev, &did))
       {
        DWORD cbRequired = 0;
        SetupDiGetDeviceInterfaceDetail(hdev, &did, 0, 0, &cbRequired, 0);
       
	    if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
         {
          PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR,cbRequired);
          
		  if (pdidd)
           {
            pdidd->cbSize = sizeof(*pdidd);
            
			if (SetupDiGetDeviceInterfaceDetail(hdev,&did,pdidd,cbRequired,&cbRequired,0))
             {
              HANDLE hBattery = CreateFile(pdidd->DevicePath,GENERIC_READ | GENERIC_WRITE,
              FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
              
			  if (INVALID_HANDLE_VALUE != hBattery)
               {
                BATTERY_QUERY_INFORMATION bqi = {0};

                DWORD dwWait = 0;
                DWORD dwOut;

                if (DeviceIoControl(hBattery,
                                    IOCTL_BATTERY_QUERY_TAG,
                                    &dwWait,
                                    sizeof(dwWait),
                                    &bqi.BatteryTag,
                                    sizeof(bqi.BatteryTag),
                                    &dwOut,
                                    NULL)
                    && bqi.BatteryTag)
                 {
                  string  bi;
                  bqi.InformationLevel = BatteryDeviceName;

                  if (DeviceIoControl(hBattery,
                                      IOCTL_BATTERY_QUERY_INFORMATION,
                                      &bqi,
                                      sizeof(bqi),
                                      &bi,
                                      sizeof(bi),
                                      &dwOut,
                                      NULL))
				{
					cout<<bi;
							
                      BATTERY_WAIT_STATUS bws = {0};
                      bws.BatteryTag = bqi.BatteryTag;

                      BATTERY_STATUS bs;
                      if (DeviceIoControl(hBattery,
                                          IOCTL_BATTERY_QUERY_STATUS,
                                          &bws,
                                          sizeof(bws),
                                          &bs,
                                          sizeof(bs),
                                          &dwOut,
                                          NULL))
                       {
                        if (bs.PowerState & BATTERY_POWER_ON_LINE)
                         {
                          dwResult &= ~GBS_ONBATTERY;
                         }
                       }
                   }
                 }
                CloseHandle(hBattery);
               }
             }
            LocalFree(pdidd);
           }
         }
       }
        else  if (ERROR_NO_MORE_ITEMS == GetLastError())
         {
          break;
         }
     }
    SetupDiDestroyDeviceInfoList(hdev);
   }

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Get battery device name

Posted 08 February 2013 - 04:13 PM

What do you mean by it's not working? It's not compiling? It's not giving you the expected output? It's crashing?
Was This Post Helpful? 0
  • +
  • -

#9 popgalop  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 65
  • Joined: 08-November 12

Re: Get battery device name

Posted 08 February 2013 - 04:17 PM

where i am using the cout statement nothing is printing the console is blank, i think it is because the if statement above it is no executing
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Get battery device name

Posted 08 February 2013 - 04:22 PM

BatteryDeviceName is expecting you to pass in a Unicode buffer to be filled by DeviceIoControl() code to build a C string. You are passing a C++ string.

Additionally, cout can only print out ANSI strings. The API above returns Unicode strings, so you'll probably need to use wcout instead.
Was This Post Helpful? 0
  • +
  • -

#11 popgalop  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 65
  • Joined: 08-November 12

Re: Get battery device name

Posted 08 February 2013 - 05:00 PM

i switched to wstring and wcout but it still wont work
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Get battery device name

Posted 08 February 2013 - 06:17 PM

C Strings
Was This Post Helpful? 0
  • +
  • -

#13 popgalop  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 65
  • Joined: 08-November 12

Re: Get battery device name

Posted 08 February 2013 - 06:29 PM

i think i got it to work but i got the name L09S6Y02 returned is this the type of name that would be returned by BatteryDeviceName or am i doing something wrong
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,396
  • Joined: 19-February 09

Re: Get battery device name

Posted 08 February 2013 - 06:45 PM

Yep, if you search online you can find that L09S6Y02 is a Lenovo laptop battery.
Was This Post Helpful? 0
  • +
  • -

#15 popgalop  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 65
  • Joined: 08-November 12

Re: Get battery device name

Posted 09 February 2013 - 05:12 AM

OK thanks for all the help i just have one last question, i am trying to get the manufacture date and print it as a date, so i took the code i had to get the batteryinformation structure and just replaced it with BatteryManufactureDate but it wasnt working anyone know why. Here is the significent part of my code, the rest is the same from eaarlier
                if (DeviceIoControl(hBattery,
                                    IOCTL_BATTERY_QUERY_TAG,
                                    &dwWait,
                                    sizeof(dwWait),
                                    &bqi.BatteryTag,
                                    sizeof(bqi.BatteryTag),
                                    &dwOut,
                                    NULL)
                    && bqi.BatteryTag)
                 {
                  BATTERY_MANUFACTURE_DATE  bi;
                  bqi.InformationLevel = BatteryManufactureDate;

                  if (DeviceIoControl(hBattery,
                                      IOCTL_BATTERY_QUERY_INFORMATION,
                                      &bqi,
                                      sizeof(bqi),
                                      &bi,
                                      sizeof(bi),
                                      &dwOut,
                                      NULL))
				{
					cout<<bi.Month<<"/"<<bi.Day<<"/"<<bi.Year;
                    BATTERY_WAIT_STATUS bws = {0};
                    bws.BatteryTag = bqi.BatteryTag;

                      BATTERY_STATUS bs;
                   }
                 }


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2