找回密码
 加入
搜索
查看: 2739|回复: 13

如何使用该API获取电池的状态

[复制链接]
发表于 2008-11-7 22:02:16 | 显示全部楼层 |阅读模式
有这样一个API函数如下
Declare Function GetSystemPowerStatus Lib "kernel32.dll" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
Type SYSTEM_POWER_STATUS
    ACLineStatus As Byte
    BatteryFlag As Byte
    BatteryLifePercent As Byte
    Reserved1 As Byte
    BatteryLifeTime As Long
    BatteryFullLifeTime As Long
End Type  
其中 ACLineStatus 为电脑使用交流还是直流的判断项。
如何获得返回该参数状态?
$x =DLLCall("kernel32.dll","int","GetSystemPowerStatus","byt ",ACLineStatus)
MsgBox(1,1,$x,1)
显示错误,请问高手,该如何描述,谢谢

[ 本帖最后由 sxpms 于 2008-11-8 22:05 编辑 ]
发表于 2008-11-7 22:22:10 | 显示全部楼层


;======================================================
;    _BatteryQuery()
;    Return information on the Battery
;    Sets @Error on error
;    Returns an array:
;        $array[0]    = ACPower(0=offline, 1=online, 255=unknown)
;        $array[1]    = BatteryFlag(1=High, 2=Low, 4=Critical,
;                      8=Charging 128=No Battery, 255=Unknown
;                      Use BitAnd to test, ie BitAnd($array[1],128)
;        $array[2]    = BatteryLife %(0-100, 255=unknown)
;        $array[3]    = Seconds left of charge, estimate(4294967295=unknown)
;======================================================

Func _BatteryQuery()
    Local $SystemPower, $ret, $array[4]

; Setup $array and $SystemPower
    $SystemPower = DllStructCreate("ubyte;ubyte;ubyte;ubyte;ulong;ulong")
    If @error Then
        SetError(-1)
        Return $array
    EndIf

; make the DllCall
    $ret = DllCall("kernel32.dll", "int", "GetSystemPowerStatus", "ptr", DllStructGetPtr($SystemPower))
    If @error Then;DllCall Failed
        SetError(-2)
        $SystemPower = 0
        Return $array
    EndIf

    If Not $ret[0] Then; GetSystemPowerStatus Failed
        SetError(-3)
        $SystemPower = 0
        Return $array
    EndIf

; Fill the array
    $array[0] = DllStructGetData($SystemPower, 1);    AC
    $array[1] = DllStructGetData($SystemPower, 2);    Battery Charge
    $array[2] = DllStructGetData($SystemPower, 3);    Battery Charge %
    $array[3] = DllStructGetData($SystemPower, 5);    Sec Battery Left

; free the struct
    $SystemPower = 0

    Return $array
EndFunc  ;==>_BatteryQuery
 楼主| 发表于 2008-11-7 22:42:04 | 显示全部楼层
谢谢你,周末愉快
 楼主| 发表于 2008-11-7 22:57:20 | 显示全部楼层
调用时,返回无数据,是我的电脑有问题吗?
$bt= _BatteryQuery()
For $t=0 to 2
MsgBox(1, 1, $bt[$t], 1)
Next
Func _BatteryQuery()
        Local $SystemPower, $ret, $array[4]

[ 本帖最后由 sxpms 于 2008-11-7 22:58 编辑 ]
 楼主| 发表于 2008-11-7 23:17:14 | 显示全部楼层
麻烦叁恨居士送人送到底, 帮我一把吧。我主要是检测我家里是否有电,无电短信告之,需要这个函数
发表于 2008-11-8 00:06:28 | 显示全部楼层
可以检测是否连接电源和电池电量的。
这里返回说得很清楚了的啦。

 ;$array[0]    = ACPower(0=offline, 1=online, 255=unknown)


$battery    = _BatteryQuery()


Switch $battery[0] 
    case  0
        MsgBox(0,0,"AC Power is Offline") 
    case  1
        MsgBox(0,0,"AC Power is OnLine") 
    case Else
        MsgBox(0,0,"AC Power is Unknown") 
EndSwitch
 楼主| 发表于 2008-11-8 22:11:10 | 显示全部楼层
我电脑使用情况返回不对,不过对API的使用有一定程度的了解,谢谢!

[ 本帖最后由 sxpms 于 2008-11-8 22:14 编辑 ]
发表于 2009-2-17 10:42:21 | 显示全部楼层
我想知道电池的电量又该怎么写了?
谢谢了!
发表于 2009-2-17 10:46:30 | 显示全部楼层
谢谢 sanhen 提供的代码!我搞定了!
发表于 2009-2-17 11:30:44 | 显示全部楼层
正好入手epc 回去研究下
发表于 2009-11-18 12:30:56 | 显示全部楼层
真是踏破铁鞋无觅处....
多谢居士!
发表于 2010-12-16 16:55:17 | 显示全部楼层
现在学这个有点吃力。LZ可以发详细一点有代码吗?3Q~~~~
发表于 2010-12-16 16:58:41 | 显示全部楼层
#Include <WinAPIEx.au3>
_WinAPI_GetSystemPowerStatus()
_WinAPI_DevicePowerSetDeviceState()
发表于 2011-12-6 20:06:20 | 显示全部楼层
回复 13# netegg


    非常感谢  O(∩_∩)O~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-19 13:47 , Processed in 0.081101 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表