Jump to content
Sign in to follow this  
targeter

DLLCall for battery info

Recommended Posts

Here are some structs that might be of use. It's way past time to recharge my own batteries.

; =================================================================================================================================
#region Capabilities Constants
#cs Indicates that the battery capacity and rate information are relative, and not in any specific units.
    If this bit is not set, the reporting units are milliwatt-hours (mWh) or milliwatts (mW) as appropriate.
    If this bit is set, all references to units in the other battery documentation can be ignored.
    All rate information is reported in units per hour. For example, if the fully charged capacity is reported as 100,
#ce a rate of 200 indicates that the battery will use all of its capacity in half an hour.
Global Const $BATTERY_CAPACITY_RELATIVE = 0x40000000

;Indicates that the normal operation is for a fail-safe function. If this bit is not set the battery is expected to be used during normal system usage.
Global Const $BATTERY_IS_SHORT_TERM = 0x20000000

;Indicates that set information requests of the type BatteryCharge are supported by this battery device.
Global Const $BATTERY_SET_CHARGE_SUPPORTED = 0x00000001

;Indicates that set information requests of the type BatteryDischarge are supported by this battery device.
Global Const $BATTERY_SET_DISCHARGE_SUPPORTED = 0x00000002

;Indicates that the battery can provide general power to run the system.
Global Const $BATTERY_SYSTEM_BATTERY = 0x80000000
#endregion Capabilities Constants

#cs typedef struct _BATTERY_INFORMATION {
    ULONG Capabilities;
    UCHAR Technology;
    UCHAR Reserved[3];
    UCHAR Chemistry[4];
    ULONG DesignedCapacity;
    ULONG FullChargedCapacity;
    ULONG DefaultAlert1;
    ULONG DefaultAlert2;
    ULONG CriticalBias;
    ULONG CycleCount;
#ce } BATTERY_INFORMATION, *PBATTERY_INFORMATION;

Global Const $Battery_Information = DllStructCreate( _
        "ulong Capabilities;" & _
        "uchar Technology;" & _
        "uchar Reserved[3];" & _
        "uchar Chemistry[4];" & _
        "ulong DesignedCapacity;" & _
        "ulong FullChargedCapacity;" & _
        "ulong DefaultAlert1;" & _
        "ulong DefaultAlert2;" & _
        "ulong CriticalBias;" & _
        "ulong CycleCount;")

Global Const $PBatteryInformation = DllStructGetPtr($Battery_Information)
; =================================================================================================================================


; =================================================================================================================================
#cs typedef enum _BATTERY_QUERY_INFORMATION_LEVEL {
    BatteryInformation = 0,
    BatteryGranularityInformation,
    BatteryTemperature,
    BatteryEstimatedTime,
    BatteryDeviceName,
    BatteryManufactureDate,
    BatteryManufactureName,
    BatteryUniqueID,
    BatterySerialNumber
#ce } BATTERY_QUERY_INFORMATION_LEVEL;

Global Enum _
    $BatteryInformation = 0, _
    $BatteryGranularityInformation, _
    $BatteryTemperature, _
    $BatteryEstimatedTime, _
    $BatteryDeviceName, _
    $BatteryManufactureDate, _
    $BatteryManufactureName, _
    $BatteryUniqueID, _
    $BatterySerialNumber
; =================================================================================================================================


; =================================================================================================================================
#cs typedef struct _BATTERY_QUERY_INFORMATION {
    ULONG BatteryTag;
    BATTERY_QUERY_INFORMATION_LEVEL InformationLevel;
    LONG    AtRate;
#ce } BATTERY_QUERY_INFORMATION, *PBATTERY_QUERY_INFORMATION;


Global Const $Battery_Query_Information = DllStructCreate("ulong BatteryTag;int InformationLevel;long AtRate")
Global Const $PBattery_Query_Information = DllStructGetPtr($Battery_Query_Information)
; =================================================================================================================================


; =================================================================================================================================
#cs typedef struct _BATTERY_MANUFACTURE_DATE {
    UCHAR Day;
    UCHAR Month;
    USHORT Year;
#ce } BATTERY_MANUFACTURE_DATE, *PBATTERY_MANUFACTURE_DATE;

Global Const $Battery_Manufacture_Date = DllStructCreate("uchar Day;uchar Month;ushort Year")
Global Const $PBattery_Manufacture_Date = DllStructGetPtr($BatteryManufactureDate)
; =================================================================================================================================


; =================================================================================================================================
#cs typedef struct {
    ULONG Granularity;
    ULONG Capacity;
#ce } BATTERY_REPORTING_SCALE, *PBATTERY_REPORTING_SCALE;

Global Const $Battery_Reporting_Scale = DllStructCreate("ulong Granularity;ulong Capacity")
Global Const $PBattery_Reporting_Scale = DllStructGetPtr($Battery_Reporting_Scale)
; =================================================================================================================================


; =================================================================================================================================
#cs typedef struct _BATTERY_SET_INFORMATION {
    ULONG   BatteryTag;
    BATTERY_SET_INFORMATION_LEVEL InformationLevel;
    UCHAR   Buffer[1];
#ce } BATTERY_SET_INFORMATION, *PBATTERY_SET_INFORMATION;

Global Const $Battery_Set_Information = DllStructCreate("ulong BatteryTag;int InformationLevel; uchar Buffer[1]")
Global Const $PBattery_Set_Information = DllStructGetPtr($Battery_Set_Information)
; =================================================================================================================================


; =================================================================================================================================
#cs typedef struct _BATTERY_STATUS {
    ULONG PowerState;
    ULONG Capacity;
    ULONG Voltage;
    LONG Rate;
#ce } BATTERY_STATUS, *PBATTERY_STATUS;

Global Const $Battery_Status = DllStructCreate("ulong PowerState;ulong Capacity;ulong Voltage;ulong Rate")
Global Const $PBattery_Status = DllStructGetPtr($Battery_Status)
; =================================================================================================================================

Share this post


Link to post
Share on other sites

Here are some structs that might be of use. It's way past time to recharge my own batteries.

Same here. I'll try this again in the morning. The main thing is it looks like all the global defs are there. I am mainly wondering how this changes up the syntax structure of the Dllcall. This is starting to get way more detailed than I originally imagined.

Yashied, if you can help me out here it would be appreciated.

Share this post


Link to post
Share on other sites

I do not have batteries, so can not verify.

#Include <WinAPI.au3>

Global Const $IOCTL_BATTERY_QUERY_INFORMATION  = 0x00294044
Global Const $IOCTL_BATTERY_QUERY_STATUS = 0x0029404C
Global Const $IOCTL_BATTERY_QUERY_TAG = 0x00294040

Global Const $BatteryInformation = 0
Global Const $BatteryGranularityInformation = 1
Global Const $BatteryTemperature = 2
Global Const $BatteryEstimatedTime = 3
Global Const $BatteryDeviceName = 4
Global Const $BatteryManufactureDate = 5
Global Const $BatteryManufactureName = 6
Global Const $BatteryUniqueID = 7
Global Const $BatterySerialNumber = 8

Global Const $BATTERY_CAPACITY_RELATIVE = 0x40000000
Global Const $BATTERY_IS_SHORT_TERM = 0x20000000
Global Const $BATTERY_SET_CHARGE_SUPPORTED = 0x00000001
Global Const $BATTERY_SET_DISCHARGE_SUPPORTED = 0x00000002
Global Const $BATTERY_SYSTEM_BATTERY = 0x80000000

;Global Const $BATTERY_UNKNOWN_... = 0xFFFFFFFF

Global Const $BATTERY_CHARGING = 0x00000004
Global Const $BATTERY_CRITICAL = 0x00000008
Global Const $BATTERY_DISCHARGING = 0x00000002
Global Const $BATTERY_POWER_ON_LINE = 0x00000001

Global Const $tagBATTERY_INFORMATION = 'ulong Capabilities;byte Technology;byte Reserved[3];char Chemistry[4];ulong DesignedCapacity;ulong FullChargedCapacity;ulong DefaultAlert1;ulong DefaultAlert2;ulong CriticalBias;ulong CycleCount'
Global Const $tagBATTERY_MANUFACTURE_DATE = 'byte Day;byte Month;ushort Year'
Global Const $tagBATTERY_QUERY_INFORMATION = 'ulong BatteryTag;ulong InformationLevel;long AtRate'
Global Const $tagBATTERY_REPORTING_SCALE = 'ulong Granularity;ulong Capacity'
Global Const $tagBATTERY_STATUS = 'ulong PowerState;ulong Capacity;ulong Voltage;long Rate'
Global Const $tagBATTERY_WAIT_STATUS = 'ulong BatteryTag;ulong Timeout;ulong PowerState;ulong LowCapacity;ulong HighCapacity'

;$sDevice = ...
$hBattery = _WINAPI_CreateFile($sDevice, 3, 2, 2)
$Tag = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_TAG, 'ulong*', -1, 'dword', 4, 'ulong*', 0, 'dword', 4, 'dword*', 0, 'ptr', 0)
$Tag = $Tag[5]
$tBATTERY_QUERY_INFORMATION = DllStructCreate($tagBATTERY_QUERY_INFORMATION)
DllStructSetData($tBATTERY_QUERY_INFORMATION, 'BatteryTag', $Tag)
DllStructSetData($tBATTERY_QUERY_INFORMATION, 'InformationLevel', $BatteryInformation)
DllStructSetData($tBATTERY_QUERY_INFORMATION, 'AtRate', 0)
$tBATTERY_INFORMATION = DllStructCreate($tagBATTERY_INFORMATION)
DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBATTERY_QUERY_INFORMATION), 'dword', DllStructGetSize($tBATTERY_QUERY_INFORMATION), 'ptr', DllStructGetPtr($tBATTERY_INFORMATION), 'dword', DllStructGetSize($tBATTERY_INFORMATION), 'dword*', 0, 'ptr', 0)
For $i = 1 To 1
    ConsoleWrite(DllStructGetData($tBATTERY_INFORMATION, $i) & @CR)
Next

Share this post


Link to post
Share on other sites

I do not have batteries, so can not verify.

#Include <WinAPI.au3>

Global Const $IOCTL_BATTERY_QUERY_INFORMATION  = 0x00294044
Global Const $IOCTL_BATTERY_QUERY_STATUS = 0x0029404C
Global Const $IOCTL_BATTERY_QUERY_TAG = 0x00294040

Global Const $BatteryInformation = 0
Global Const $BatteryGranularityInformation = 1
Global Const $BatteryTemperature = 2
Global Const $BatteryEstimatedTime = 3
Global Const $BatteryDeviceName = 4
Global Const $BatteryManufactureDate = 5
Global Const $BatteryManufactureName = 6
Global Const $BatteryUniqueID = 7
Global Const $BatterySerialNumber = 8

Global Const $BATTERY_CAPACITY_RELATIVE = 0x40000000
Global Const $BATTERY_IS_SHORT_TERM = 0x20000000
Global Const $BATTERY_SET_CHARGE_SUPPORTED = 0x00000001
Global Const $BATTERY_SET_DISCHARGE_SUPPORTED = 0x00000002
Global Const $BATTERY_SYSTEM_BATTERY = 0x80000000

;Global Const $BATTERY_UNKNOWN_... = 0xFFFFFFFF

Global Const $BATTERY_CHARGING = 0x00000004
Global Const $BATTERY_CRITICAL = 0x00000008
Global Const $BATTERY_DISCHARGING = 0x00000002
Global Const $BATTERY_POWER_ON_LINE = 0x00000001

Global Const $tagBATTERY_INFORMATION = 'ulong Capabilities;byte Technology;byte Reserved[3];char Chemistry[4];ulong DesignedCapacity;ulong FullChargedCapacity;ulong DefaultAlert1;ulong DefaultAlert2;ulong CriticalBias;ulong CycleCount'
Global Const $tagBATTERY_MANUFACTURE_DATE = 'byte Day;byte Month;ushort Year'
Global Const $tagBATTERY_QUERY_INFORMATION = 'ulong BatteryTag;ulong InformationLevel;long AtRate'
Global Const $tagBATTERY_REPORTING_SCALE = 'ulong Granularity;ulong Capacity'
Global Const $tagBATTERY_STATUS = 'ulong PowerState;ulong Capacity;ulong Voltage;long Rate'
Global Const $tagBATTERY_WAIT_STATUS = 'ulong BatteryTag;ulong Timeout;ulong PowerState;ulong LowCapacity;ulong HighCapacity'

;$sDevice = ...
$hBattery = _WINAPI_CreateFile($sDevice, 3, 2, 2)
$Tag = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_TAG, 'ulong*', -1, 'dword', 4, 'ulong*', 0, 'dword', 4, 'dword*', 0, 'ptr', 0)
$Tag = $Tag[5]
$tBATTERY_QUERY_INFORMATION = DllStructCreate($tagBATTERY_QUERY_INFORMATION)
DllStructSetData($tBATTERY_QUERY_INFORMATION, 'BatteryTag', $Tag)
DllStructSetData($tBATTERY_QUERY_INFORMATION, 'InformationLevel', $BatteryInformation)
DllStructSetData($tBATTERY_QUERY_INFORMATION, 'AtRate', 0)
$tBATTERY_INFORMATION = DllStructCreate($tagBATTERY_INFORMATION)
DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBATTERY_QUERY_INFORMATION), 'dword', DllStructGetSize($tBATTERY_QUERY_INFORMATION), 'ptr', DllStructGetPtr($tBATTERY_INFORMATION), 'dword', DllStructGetSize($tBATTERY_INFORMATION), 'dword*', 0, 'ptr', 0)
For $i = 1 To 1
    ConsoleWrite(DllStructGetData($tBATTERY_INFORMATION, $i) & @CR)
Next

Actually quick question is there a reason that you commented out the $sdevice file? I added Local $sdevice = @ScriptDir & "\temp.au3"....

I didn't actually get any output. Anyone else out there with a laptop that can try this out?

One other question Yashied, is there a way to store the info in an array without having to do this as an include?

Edited by targeter

Share this post


Link to post
Share on other sites

Here is how to get the values for $IOCTL_BATTERY_QUERY_INFORMATION, $IOCTL_BATTERY_QUERY_STATUS, $IOCTL_BATTERY_QUERY_TAG ...

; =================================================================================================================================
#region Device Type Constants
Global Const $FILEDEVICE_BEEP = 0x0001
Global Const $FILEDEVICE_CDROM = 0x0002
Global Const $FILEDEVICE_CDROMFILESYSTEM = 0x0003
Global Const $FILEDEVICE_CONTROLLER = 0x0004
Global Const $FILEDEVICE_DATALINK = 0x0005
Global Const $FILEDEVICE_DFS = 0x0006
Global Const $FILEDEVICE_DISK = 0x0007
Global Const $FILEDEVICE_DISKFILESYSTEM = 0x0008
Global Const $FILEDEVICE_FILESYSTEM = 0x0009
Global Const $FILEDEVICE_INPORTPORT = 0x000a
Global Const $FILEDEVICE_KEYBOARD = 0x000b
Global Const $FILEDEVICE_MAILSLOT = 0x000c
Global Const $FILEDEVICE_MIDIIN = 0x000d
Global Const $FILEDEVICE_MIDIOUT = 0x000e
Global Const $FILEDEVICE_MOUSE = 0x000f
Global Const $FILEDEVICE_MULTIUNCPROVIDER = 0x0010
Global Const $FILEDEVICE_NAMEDPIPE = 0x0011
Global Const $FILEDEVICE_NETWORK = 0x0012
Global Const $FILEDEVICE_NETWORKBROWSER = 0x0013
Global Const $FILEDEVICE_NETWORKFILESYSTEM = 0x0014
Global Const $FILEDEVICE_NULL = 0x0015
Global Const $FILEDEVICE_PARALLELPORT = 0x0016
Global Const $FILEDEVICE_PHYSICALNETCARD = 0x0017
Global Const $FILEDEVICE_PRINTER = 0x0018
Global Const $FILEDEVICE_SCANNER = 0x0019
Global Const $FILEDEVICE_SERIALMOUSEPORT = 0x001a
Global Const $FILEDEVICE_SERIALPORT = 0x001b
Global Const $FILEDEVICE_SCREEN = 0x001c
Global Const $FILEDEVICE_SOUND = 0x001d
Global Const $FILEDEVICE_STREAMS = 0x001e
Global Const $FILEDEVICE_TAPE = 0x001f
Global Const $FILEDEVICE_TAPEFILESYSTEM = 0x0020
Global Const $FILEDEVICE_TRANSPORT = 0x0021
Global Const $FILEDEVICE_UNKNOWN = 0x0022
Global Const $FILEDEVICE_VIDEO = 0x0023
Global Const $FILEDEVICE_VIRTUALDISK = 0x0024
Global Const $FILEDEVICE_WAVEIN = 0x0025
Global Const $FILEDEVICE_WAVEOUT = 0x0026
Global Const $FILEDEVICE_8042PORT = 0x0027
Global Const $FILEDEVICE_NETWORKREDIRECTOR = 0x0028
Global Const $FILEDEVICE_BATTERY = 0x0029
Global Const $FILEDEVICE_BUSEXTENDER = 0x002a
Global Const $FILEDEVICE_MODEM = 0x002b
Global Const $FILEDEVICE_VDM = 0x002c
Global Const $FILEDEVICE_MASSSTORAGE = 0x002d
Global Const $FILEDEVICE_SMB = 0x002e
Global Const $FILEDEVICE_KS = 0x002f
Global Const $FILEDEVICE_CHANGER = 0x0030
Global Const $FILEDEVICE_SMARTCARD = 0x0031
Global Const $FILEDEVICE_ACPI = 0x0032
Global Const $FILEDEVICE_DVD = 0x0033
Global Const $FILEDEVICE_FULLSCREENVIDEO = 0x0034
Global Const $FILEDEVICE_DFSFILESYSTEM = 0x0035
Global Const $FILEDEVICE_DFSVOLUME = 0x0036
Global Const $FILEDEVICE_SERENUM = 0x0037
Global Const $FILEDEVICE_TERMSRV = 0x0038
Global Const $FILEDEVICE_KSEC = 0x0039
#endregion Device Type Constants

#region Method Constants
Global Const $METHOD_BUFFERED = 0x00
Global Const $METHOD_INDIRECT = 0x01
Global Const $METHOD_OUTDIRECT = 0x02
Global Const $METHOD_NEITHER = 0x03
#endregion Method Constants

#region Access
Global Const $FILEACCESS_ANY = 0x00 ; Request all access.
Global Const $FILEACCESS_READ = 0x01 ; Request read access. Can be used with FILE_WRITE_ACCESS.
Global Const $FILEACCESS_WRITE = 0x02; Request write access. Can be used with FILE_READ_ACCESS.
Global Const $FILEACCESS_READWRITE = 0x03
#endregion Access

Func CTL_Code(Const $DeviceType, Const $Function, Const $Method, Const $Access = $FILEACCESS_ANY)

    Select
        Case ($DeviceType > 65535) Or ($DeviceType < 0)
            Return SetError(-1, 0, 1)
        Case ($Function > 4095) Or ($Function < 0)
            Return SetError(-2, 0, 1)
        Case ($Method < 0) Or ($Method > 3)
            Return SetError(-3, 0, 1)
        Case ($Method < 0) Or ($Access > 3)
            Return SetError(-4, 0, 1)
    EndSelect

    #cs #define CTL_CODE(DeviceType, Function, Method, Access) (
        ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)
    #ce )

    Local Const $CTL_CODE = BitOR(BitShift($DeviceType, -16), BitShift($Access, -14), BitShift($Function, -2), $Method)

    Return $CTL_CODE
EndFunc ;==>CTL_Code
; =================================================================================================================================
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

New Version of _BatteryInfo(). Too bad I don't have my laptop! I may be able to borrow someone's netbook.

I'm going to stop this until I can get my laptop back...

#include <WinAPI.au3>
#include <Array.au3>

_BatteryInfo()

ConsoleWrite("@error: " & @error & @LF)

Func _BatteryInfo()

    Local Const $dFile = "\\.\batt"
    Local Const $hDevice = _WinAPI_CreateFile($dFile, 2, 6, 6)

    Local Const $IOCTL_BATTERY_QUERY_TAG = CTL_CODE($FILEDEVICE_BATTERY, 0x11)

    Local Const $BatteryQueryInformation_SerialNumber = 8

    Local Const $tagBatteryQueryInformtion = "ulong BatteryTag;int InformationLevel;long AtRate"
    Local Const $BatteryQueryInformation = DllStructCreate($tagBatteryQueryInformtion)
    Local Const $PBatteryQueryInformation = DllStructGetPtr($BatteryQueryInformation)
    Local Const $BatteryQueryInfoSize = DllStructGetSize($BatteryQueryInformation)

    Local Const $oBuffStruct = DllStructCreate("dword oBuff")
    DllStructSetData($oBuffStruct, "oBuff", 256)
    Local Const $outBuffer = DllStructGetPtr($oBuffStruct)
    Local Const $outBufferSize = DllStructGetSize($oBuffStruct)

    Local Const $bytesReturned = DllStructCreate("dword BytesReturned")
    DllStructSetData($bytesReturned, "bytesReturned", 256)
    Local Const $pBytesReturned = DllStructGetPtr($bytesReturned)

    #cs BOOL WINAPI DeviceIoControl(
        __in    HANDLE hDevice,
        __in    DWORD dwIoControlCode,
        __in_opt    LPVOID lpInBuffer,
        __in    DWORD nInBufferSize,
        __out_opt LPVOID lpOutBuffer,
        __in    DWORD nOutBufferSize,
        __out_opt LPDWORD lpBytesReturned,
    #ce __inout_opt LPOVERLAPPED lpOverlapped );

    Local Const $kernel32 = DllOpen("Kernel32.dll")

    ; make the DllCall for battery information
    Local Const $ret2 = DllCall("kernel32.dll", "bool", "DeviceIoControl", _
            "hwnd", $hDevice, _
            "dword", $IOCTL_BATTERY_QUERY_TAG, _
            "ptr", $PBatteryQueryInformation, _
            "dword", $BatteryQueryInfoSize, _
            "ptr", $outBuffer, _
            "dword", $outBufferSize, _
            "ptr", $pBytesReturned, _
            "int", 0)

    DllClose($kernel32)

    _WinAPI_CloseHandle($hDevice)

    ; DllCall Failed
    If @error Then Return SetError(-1, 0, 1)

    ; Query Failed
    If Not IsArray($ret2) Then Return SetError(-2, 0, 1)

    _ArrayDisplay($ret2)

    ConsoleWrite(@LF & "===============================================================" & @LF)
    ConsoleWrite("DeviceIoControl(): " & @LF)
    ConsoleWrite(@TAB & "BatteryStatus: " & DllStructGetData($BatteryQueryInformation, "BatteryStatus") & @LF)
    ConsoleWrite(@TAB & "InformationLevel: " & DllStructGetData($BatteryQueryInformation, "InformationLevel") & @LF)
    ConsoleWrite(@TAB & "AtRate: " & DllStructGetData($BatteryQueryInformation, "AtRate") & @LF)



    ConsoleWrite("oBuffStruct(): " & @LF)
    ConsoleWrite(@TAB & "oBuff: " & DllStructGetData($bytesReturned, "oBuff") & @LF)



    ConsoleWrite("BytesReturned(): " & @LF)
    ConsoleWrite(@TAB & "BytesReturned: " & DllStructGetData($bytesReturned, "BytesReturned") & @LF)
    ConsoleWrite("===============================================================" & @LF)

    Return $ret2
EndFunc ;==>_BatteryInfo
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

What is "\\.\batt"?

I dunno, it was in his original post. I assume it is a device handle to a battery?

I think that both Targeter and I are kinda new to this so definately if he wishes to continue the discussion we should probably do so off forum.

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

That works.

#Include <WinAPI.au3>

Global Const $GUID_DEVCLASS_BATTERY = '{72631E54-78A4-11D0-BCF7-00AA00B7B32A}'

Global Const $DIGCF_ALLCLASSES = 0x04
Global Const $DIGCF_DEVICEINTERFACE = 0x10
Global Const $DIGCF_DEFAULT = 0x01
Global Const $DIGCF_PRESENT = 0x02
Global Const $DIGCF_PROFILE = 0x08

Global Const $IOCTL_BATTERY_QUERY_INFORMATION  = 0x00294044
Global Const $IOCTL_BATTERY_QUERY_STATUS = 0x0029404C
Global Const $IOCTL_BATTERY_QUERY_TAG = 0x00294040

Global Const $BatteryInformation = 0
Global Const $BatteryGranularityInformation = 1
Global Const $BatteryTemperature = 2
Global Const $BatteryEstimatedTime = 3
Global Const $BatteryDeviceName = 4
Global Const $BatteryManufactureDate = 5
Global Const $BatteryManufactureName = 6
Global Const $BatteryUniqueID = 7
Global Const $BatterySerialNumber = 8

Global Const $BATTERY_CAPACITY_RELATIVE = 0x40000000
Global Const $BATTERY_IS_SHORT_TERM = 0x20000000
Global Const $BATTERY_SET_CHARGE_SUPPORTED = 0x00000001
Global Const $BATTERY_SET_DISCHARGE_SUPPORTED = 0x00000002
Global Const $BATTERY_SYSTEM_BATTERY = 0x80000000

Global Const $BATTERY_CHARGING = 0x00000004
Global Const $BATTERY_CRITICAL = 0x00000008
Global Const $BATTERY_DISCHARGING = 0x00000002
Global Const $BATTERY_POWER_ON_LINE = 0x00000001

Global Const $tagSP_DEVINFO_DATA = 'dword Size;' & $tagGUID & ';dword DevInst;ulong_ptr Reserved'
Global Const $tagSP_DEVICE_INTERFACE_DATA = 'dword Size;' & $tagGUID & ';dword Flag;ulong_ptr Reserved'
Global Const $tagSP_DEVICE_INTERFACE_DETAIL_DATA = 'dword Size;wchar DevicePath[1024]'

Global Const $tagBATTERY_INFORMATION = 'ulong Capabilities;byte Technology;byte Reserved[3];char Chemistry[4];ulong DesignedCapacity;ulong FullChargedCapacity;ulong DefaultAlert1;ulong DefaultAlert2;ulong CriticalBias;ulong CycleCount'
Global Const $tagBATTERY_MANUFACTURE_DATE = 'byte Day;byte Month;ushort Year'
Global Const $tagBATTERY_QUERY_INFORMATION = 'ulong BatteryTag;ulong InformationLevel;long AtRate'
Global Const $tagBATTERY_REPORTING_SCALE = 'ulong Granularity;ulong Capacity'
Global Const $tagBATTERY_STATUS = 'ulong PowerState;ulong Capacity;ulong Voltage;long Rate'
Global Const $tagBATTERY_WAIT_STATUS = 'ulong BatteryTag;ulong Timeout;ulong PowerState;ulong LowCapacity;ulong HighCapacity'

$aData = _QueryBatteryInfo()

If IsArray($aData) Then
    ConsoleWrite('BatteryName:         ' & $aData[0 ] & @CR)
    ConsoleWrite('ManufactureName:     ' & $aData[1 ] & @CR)
    ConsoleWrite('ManufactureDate:     ' & $aData[2 ] & @CR)
    ConsoleWrite('SerialNumber:        ' & $aData[3 ] & @CR)
    ConsoleWrite('UniqueID:            ' & $aData[4 ] & @CR)
    ConsoleWrite('Temperature:         ' & $aData[5 ] & @CR)
    ConsoleWrite('Capabilities:        ' & $aData[6 ] & @CR)
    ConsoleWrite('Technology:          ' & $aData[7 ] & @CR)
    ConsoleWrite('Chemistry:           ' & $aData[8 ] & @CR)
    ConsoleWrite('DesignedCapacity:    ' & $aData[9 ] & @CR)
    ConsoleWrite('FullChargedCapacity: ' & $aData[10] & @CR)
    ConsoleWrite('DefaultAlert1:       ' & $aData[11] & @CR)
    ConsoleWrite('DefaultAlert2:       ' & $aData[12] & @CR)
    ConsoleWrite('CriticalBias:        ' & $aData[13] & @CR)
    ConsoleWrite('CycleCount:          ' & $aData[14] & @CR)
Else
    ConsoleWrite('Battery not found.' & @CR)
EndIf

Func _QueryBatteryInfo($iBattery = 0)

    Local $tGUID = _WinAPI_GUIDFromString($GUID_DEVCLASS_BATTERY)

    If Not IsDllStruct($tGUID) Then
        Return 0
    EndIf

    Local $tSPDID = DllStructCreate($tagSP_DEVICE_INTERFACE_DATA)
    Local $pSPDID = DllStructGetPtr($tSPDID)
    Local $tSPDIDD = DllStructCreate($tagSP_DEVICE_INTERFACE_DETAIL_DATA)
    Local $pGUID = DllStructGetPtr($tGUID)
    Local $hData, $Tag, $Ret, $Err = 1

    $Ret = DllCall('setupapi.dll', 'ptr', 'SetupDiGetClassDevsW', 'ptr', $pGUID, 'ptr', 0, 'ptr', 0, 'dword', BitOR($DIGCF_DEVICEINTERFACE, $DIGCF_PRESENT))
    If (@error) Or (Not $Ret[0]) Then
        Return 0
    EndIf
    $hData = $Ret[0]
    DllStructSetData($tSPDID, 'Size', DllStructGetSize($tSPDID))
    If @AutoItX64 Then
        DllStructSetData($tSPDIDD, 'Size', 8)
    Else
        DllStructSetData($tSPDIDD, 'Size', 6)
    EndIf
    Do
        $Ret = Dllcall('setupapi.dll', 'int', 'SetupDiEnumDeviceInterfaces', 'ptr', $hData, 'ptr', 0, 'ptr', $pGUID, 'dword', $iBattery, 'ptr', $pSPDID)
        If (@error) Or (Not $Ret[0]) Then
            ExitLoop
        EndIf
        $Ret = DllCall('setupapi.dll', 'int', 'SetupDiGetDeviceInterfaceDetailW', 'ptr', $hData, 'ptr', $pSPDID, 'ptr', DllStructGetPtr($tSPDIDD), 'dword', DllStructGetSize($tSPDIDD), 'ptr', 0, 'ptr', 0)
        If (@error) Or (Not $Ret[0]) Then
            ExitLoop
        EndIf
        $Err = 0
    Until 1
    Dllcall('setupapi.dll', 'int', 'SetupDiDestroyDeviceInfoList', 'ptr', $hData)
    If $Err Then
        Return 0
    EndIf

    Local $hBattery = _WINAPI_CreateFile(DllStructGetData($tSPDIDD, 'DevicePath'), 3, 2, 2)

    If Not $hBattery Then
        Return 0
    EndIf

    Local $tBQI = DllStructCreate($tagBATTERY_QUERY_INFORMATION)
    Local $tBI  = DllStructCreate($tagBATTERY_INFORMATION)
    Local $tBMD = DllStructCreate($tagBATTERY_MANUFACTURE_DATE)
    Local $tData = DllStructCreate('wchar[1024]')
    Local $aData[15]

    For $i = 0 To 14
        $aData[$i] = ''
    Next

    $Err = 1

    Do
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_TAG, 'ulong*', -1, 'dword', 4, 'ulong*', 0, 'dword', 4, 'dword*', 0, 'ptr', 0)
        If (@error) Or (Not $Ret[0]) Then
            ExitLoop
        EndIf
        $Tag = $Ret[5]
        DllStructSetData($tBQI, 'BatteryTag', $Tag)
        DllStructSetData($tBQI, 'AtRate', 0)
        DllStructSetData($tBQI, 'InformationLevel', $BatteryDeviceName)
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBQI), 'dword', DllStructGetSize($tBQI), 'ptr', DllStructGetPtr($tData), 'dword', DllStructGetSize($tData), 'dword*', 0, 'ptr', 0)
        If $Ret[0] Then
            $aData[0] = DllStructGetData($tData, 1)
        EndIf
        DllStructSetData($tBQI, 'InformationLevel', $BatteryManufactureName)
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBQI), 'dword', DllStructGetSize($tBQI), 'ptr', DllStructGetPtr($tData), 'dword', DllStructGetSize($tData), 'dword*', 0, 'ptr', 0)
        If $Ret[0] Then
            $aData[1] = DllStructGetData($tData, 1)
        EndIf
        DllStructSetData($tBQI, 'InformationLevel', $BatteryManufactureDate)
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBQI), 'dword', DllStructGetSize($tBQI), 'ptr', DllStructGetPtr($tBMD), 'dword', DllStructGetSize($tBMD), 'dword*', 0, 'ptr', 0)
        If $Ret[0] Then
            $aData[2] = StringFormat('%02d/%02d/%04d', DllStructGetData($tBMD, 'Month'), DllStructGetData($tBMD, 'Day'), DllStructGetData($tBMD, 'Year'))
        EndIf
        DllStructSetData($tBQI, 'InformationLevel', $BatterySerialNumber)
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBQI), 'dword', DllStructGetSize($tBQI), 'ptr', DllStructGetPtr($tData), 'dword', DllStructGetSize($tData), 'dword*', 0, 'ptr', 0)
        If $Ret[0] Then
            $aData[3] = DllStructGetData($tData, 1)
        EndIf
        DllStructSetData($tBQI, 'InformationLevel', $BatteryUniqueID)
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBQI), 'dword', DllStructGetSize($tBQI), 'ptr', DllStructGetPtr($tData), 'dword', DllStructGetSize($tData), 'dword*', 0, 'ptr', 0)
        If $Ret[0] Then
            $aData[4] = DllStructGetData($tData, 1)
        EndIf
        DllStructSetData($tBQI, 'InformationLevel', $BatteryTemperature)
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBQI), 'dword', DllStructGetSize($tBQI), 'ulong*', 0, 'dword', 4, 'dword*', 0, 'ptr', 0)
        If $Ret[0] Then
            $aData[5] = $Ret[5]
        EndIf
        DllStructSetData($tBQI, 'InformationLevel', $BatteryInformation)
        $Ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', 'ptr', $hBattery, 'dword', $IOCTL_BATTERY_QUERY_INFORMATION, 'ptr', DllStructGetPtr($tBQI), 'dword', DllStructGetSize($tBQI), 'ptr', DllStructGetPtr($tBI), 'dword', DllStructGetSize($tBI), 'dword*', 0, 'ptr', 0)
        If $Ret[0] Then
            For $i = 6 To 14
                $aData[$i] = DllStructGetData($tBI, $i - 5 + ($i > 7))
            Next
        EndIf
        $Err = 0
    Until 1
    _WinAPI_CloseHandle($hBattery)
    If Not $Err Then
        Return $aData
    Else
        Return 0
    EndIf
EndFunc   ;==>_QueryBatteryInfo
Edited by Yashied

Share this post


Link to post
Share on other sites

Holy moly, this is awesome. Thanks Yashied.

One last request if possible. Can you add another DLLCall for the Battery_Status structure? http://msdn.microsoft.com/en-us/library/aa372671(v=VS.85).aspx The last thing I am trying to extract is the rate of charge.....

Also, if you can add back the arate data that would be appreciated... I am trying to figure out how to add this back in since you just 0 out the arate...

Edited by targeter

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

I am actually using a modified _batteryinfo udf for that. However, I wanted to get the rate of discharge data specifically that seems only to be in the battery_status structure.

//////////////

Oh I see you mean instead of calling the estimatedruntime thing back in... Your right. That is much better.

The only thing I still wanted to grab was the rate data.... whr units, I want to use this to track performance on some ups units.

//// trying to play with the code to get the battery wait status input for the battery status info...

Edited by targeter

Share this post


Link to post
Share on other sites

OK. This UDF from me.

Battery UDF Library v0.1

Battery.au3

Example1

#Include <Battery.au3>

Opt('MustDeclareVars', 1)

Global $aData, $iTag, $sDevicePath = _Battery_GetDevicePath()

$iTag = _Battery_GetTag($sDevicePath)
If $iTag Then
    $aData = _Battery_QueryInfo($sDevicePath, $iTag)
    If IsArray($aData) Then
        ConsoleWrite('Battery name:          ' & $aData[0 ] & @CR)
        ConsoleWrite('Manufacture name:      ' & $aData[1 ] & @CR)
        ConsoleWrite('Manufacture date:      ' & $aData[2 ] & @CR)
        ConsoleWrite('Serial number:         ' & $aData[3 ] & @CR)
        ConsoleWrite('Unique ID:             ' & $aData[4 ] & @CR)
        ConsoleWrite('Temperature:           ' & $aData[5 ] & @CR)
        ConsoleWrite('Estimated time:        ' & $aData[6 ] & @CR)
        ConsoleWrite('Capabilities:          ' & $aData[7 ] & @CR)
        ConsoleWrite('Technology:            ' & $aData[8 ] & @CR)
        ConsoleWrite('Chemistry:             ' & $aData[9 ] & @CR)
        ConsoleWrite('Designed capacity:     ' & $aData[10] & @CR)
        ConsoleWrite('Full charged capacity: ' & $aData[11] & @CR)
        ConsoleWrite('Default alert1:        ' & $aData[12] & @CR)
        ConsoleWrite('Default alert2:        ' & $aData[13] & @CR)
        ConsoleWrite('Critical bias:         ' & $aData[14] & @CR)
        ConsoleWrite('Cycle count:           ' & $aData[15] & @CR)
    Else
        Switch @error
            Case 1
                ConsoleWrite('Unable to open the battery device.' & @CR)
            Case 2
                ConsoleWrite('The specified tag does not match that of the current battery tag.' & @CR)
        EndSwitch
    EndIf
Else
    ConsoleWrite('Battery not found.' & @CR)
EndIf

Example2

#Include <Battery.au3>

Opt('MustDeclareVars', 1)

Global $aData, $iTag, $sDevicePath = _Battery_GetDevicePath()

$iTag = _Battery_GetTag($sDevicePath)
If $iTag Then
    $aData = _Battery_QueryStatus($sDevicePath, $iTag)
    If IsArray($aData) Then
        ConsoleWrite('Power state: ' & $aData[0] & @CR)
        ConsoleWrite('Capacity:    ' & $aData[1] & @CR)
        ConsoleWrite('Voltage:     ' & $aData[2] & @CR)
        ConsoleWrite('Rate:        ' & $aData[3] & @CR)
    Else
        Switch @error
            Case 1
                ConsoleWrite('Unable to open the battery device.' & @CR)
            Case 2
                ConsoleWrite('The specified tag does not match that of the current battery tag.' & @CR)
        EndSwitch
    EndIf
Else
    ConsoleWrite('Battery not found.' & @CR)
EndIf

Note

You should check @error flag after every call _Battery_QueryInfo() or _Battery_QueryStatus() function. If one of these functions fails with @error = 2, you must retrieve a new tag by calling the _Battery_GetTag() function.

Enjoy.

;)

Edited by Yashied

Share this post


Link to post
Share on other sites

I'm just curious... under what sort of situation would that occur... other than the general freak windows error?

Could it actually pull the correct one and the second check actually is wrong?

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

I don't understand you.

;)

If I get the error flag 2, regarding the incorrect battery tag information not matching?

Seperately, this is what I am trying to attempt with the script. Everytime I run the script, a log file would be opened and another line of data would be generated that has the QueryStatus info in a csv format.

So the batterylog.txt file would read

Powerstatus, Capacity, Voltage, Rate

Powerstatus, Capacity, Voltage, Rate

and so on

every new line would be added everytime the script is run.

I know I need to use the following

$file1 = FileOpen("batterylog.txt", 1);1 = Read mode

:/// stuck here because I am not sure how to resolve the consolewrite to the filewriteline command without resorting to the array, hence my question about the array

FileClose($file1)

Edited by targeter

Share this post


Link to post
Share on other sites

FileWriteLine ($file1, $aData[0] & "," & $aData[1] & "," & $aData[2] & "," & $aData[3] ) Eureka!!!

The main thing was declaring the $file1. Thanks a bunch Yashied. this is a life saver.

Share this post


Link to post
Share on other sites

Because each battery device represents a slot into which a battery can be inserted, there must be a way to determine when the battery is removed and reinserted, replaced, or changed in any other way. To do this, each battery in a particular slot is assigned a tag. This tag must be used for all queries for information. If the tag provided by the application does not match the battery, the query fails, indicating to the application that the battery has changed in some way. To successfully complete the query, a new battery tag is required.

A change in the battery tag does not necessarily mean that the battery was removed and reinserted or replaced. A new tag can be generated if there is a change in any of the data that would normally be static. For example, when a battery is done charging, the last fully charged capacity may have changed. The tag can also change if battery communication was temporarily lost or if there was an improper notification from the BIOS. On some systems, the battery tag may be updated whenever the AC status changes. This behavior is due to a characteristic of the battery system and isn't common.

Whenever the battery tag is updated, the battery should be treated as if it were a new battery and all cached data should be re-read. If an application needs to know if the same physical battery is present, it should check the "BatteryUniqueID" value returned by _Battery_QueryInfo() function.

...

$DevicePath = _Battery_GetDevicePath()
$Update = 1

While 1
    If $Update Then
        $Tag = _Battery_GetTag($DevicePath)
        $Info = _Battery_QueryInfo($DevicePath, $Tag)
        If @error Then
            ExitLoop
        EndIf
        
        ...
        
        $Update = 0
    EndIf
    
    ...
    
    $Status = _Battery_QueryStatus($DevicePath, $iTag)
    Switch @error
        Case 0
            ; OK
        Case 2
            $Update = 1
        Case Else
            ExitLoop
    EndSwitch
    
    ...
    
WEnd

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...