Что нового

_QueryBatteryInfo() - Полученте информации о батарее ноутбука

Статус
Закрыто для дальнейших ответов.

Yashied

Модератор
Команда форума
Глобальный модератор
Сообщения
5 379
Репутация
2 711
Функция возвращает всю информацию (массив), какую только можно получить, о батарее вашего ноутбука (теоретически должна работать и с UPS'ами). Пол дня пришлось читать MSDN, чтобы это написать... Да, информацию о статусе с помощью _QueryBatteryInfo() получить нельзя. Для этого лучше использовать _WinAPI_GetSystemPowerStatus() из WinAPIEx.au3.

Код:
#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
 
Автор
Yashied

Yashied

Модератор
Команда форума
Глобальный модератор
Сообщения
5 379
Репутация
2 711
Тема закрыта, поскольку данная функция присутствует в библиотеке Battery.au3 и дальнейшее обсуждение здесь является бессмысленным.
 
Статус
Закрыто для дальнейших ответов.
Верх