Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: Elemental Code en 19 Agosto 2011, 18:19 pm



Título: Problema con la API GetSystemPowerStatus
Publicado por: Elemental Code en 19 Agosto 2011, 18:19 pm
HOLA!!! :D

Les dejo el codigo de la api segun la apiguide 3.7:

Código
  1. Private Type SYSTEM_POWER_STATUS
  2.        ACLineStatus As Byte
  3.        BatteryFlag As Byte
  4.        BatteryLifePercent As Byte
  5.        Reserved1 As Byte
  6.        BatteryLifeTime As Long
  7.        BatteryFullLifeTime As Long
  8. End Type
  9. Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
  10. Private Sub Form_Paint()
  11.    'KPD-Team 2000
  12.    'URL: http://www.allapi.net/
  13.    'E-Mail: KPDTeam@Hotmail.com
  14.    Dim SPS As SYSTEM_POWER_STATUS
  15.    'get the battery powerstatus
  16.    GetSystemPowerStatus SPS
  17.    Me.AutoRedraw = True
  18.    'show some information
  19.    Select Case SPS.ACLineStatus
  20.        Case 0
  21.            Me.Print "AC power status: Offline"
  22.        Case 1
  23.            Me.Print "AC power status: OnLine"
  24.        Case 2
  25.            Me.Print "AC power status: Unknown"
  26.    End Select
  27.    Select Case SPS.BatteryFlag
  28.        Case 1
  29.            Me.Print "Battery charge status: High"
  30.        Case 2
  31.            Me.Print "Battery charge status: Low"
  32.        Case 4
  33.            Me.Print "Battery charge status: Critical"
  34.        Case 8
  35.            Me.Print "Battery charge status: Charging"
  36.        Case 128
  37.            Me.Print "Battery charge status: No system battery"
  38.        Case 255
  39.            Me.Print "Battery charge status: Unknown Status"
  40.    End Select
  41. End Sub
  42.  

El problema es que si tengo la bateria cargando me devuelve 9 como flag y no 8 :S.
Saben porque sera?

Muchas gracias por su ayuda :)


Título: Re: Problema con la API GetSystemPowerStatus
Publicado por: 79137913 en 19 Agosto 2011, 18:24 pm
HOLA!!!

SO' BOLUDO' ?

XD

8 de bateria cargando
+
1 de bateria con carga alta
=
9


Pone asi y va a funcionar:
Código
  1. Private Type SYSTEM_POWER_STATUS
  2.        ACLineStatus As Byte
  3.        BatteryFlag As Byte
  4.        BatteryLifePercent As Byte
  5.        Reserved1 As Byte
  6.        BatteryLifeTime As Long
  7.        BatteryFullLifeTime As Long
  8. End Type
  9. Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
  10. Private Sub Form_Paint()
  11.    'KPD-Team 2000
  12.    'URL: http://www.allapi.net/
  13.    'E-Mail: KPDTeam@Hotmail.com
  14.    Dim SPS As SYSTEM_POWER_STATUS
  15.    'get the battery powerstatus
  16.    GetSystemPowerStatus SPS
  17.    Me.AutoRedraw = True
  18.    'show some information
  19.    Select Case SPS.ACLineStatus
  20.        Case 0
  21.            Me.Print "AC power status: Offline"
  22.        Case 1
  23.            Me.Print "AC power status: OnLine"
  24.        Case 2
  25.            Me.Print "AC power status: Unknown"
  26.    End Select
  27.    If SPS.BatteryFlag And 1 Then Me.Print "Battery charge status: High"
  28.    If SPS.BatteryFlag And 2 Then Me.Print "Battery charge status: Low"
  29.    If SPS.BatteryFlag And 4 Then Me.Print "Battery charge status: Critical"
  30.    If SPS.BatteryFlag And 8 Then Me.Print "Battery charge status: Charging"
  31.    If SPS.BatteryFlag And 128 Then Me.Print "Battery charge status: No system battery"
  32.    If SPS.BatteryFlag And 255 Then Me.Print "Battery charge status: Unknown Status"
  33. End Sub

GRACIAS POR LEER!!!


Título: Re: Problema con la API GetSystemPowerStatus
Publicado por: Elemental Code en 20 Agosto 2011, 01:03 am
y como iba a saber yo que se sumaban?  :P

Gracias mi buen amigo :)

PD: como usaste el and? no entiendi :P


Título: Re: Problema con la API GetSystemPowerStatus
Publicado por: raul338 en 20 Agosto 2011, 01:13 am
@Elemental Code

Operar a nivel de bits (http://www.elguille.info/net/dotnet/operar_con_bits.aspx)

:P


Título: Re: Problema con la API GetSystemPowerStatus
Publicado por: BlackZeroX en 21 Agosto 2011, 06:41 am
.
Esta mal ese trato de flags...(Me dio un sape 79137913) esta bien la operacion de bits,

* Se igualan a los flags con los que se realizan las operaciones en algunos casos...

la suma no se hace como normalmente se hace... se hace de manera binaria...

0000 0000 0000 0001 = Battery charge status: High
0000 0000 0000 0010 = Battery charge status: Low
0000 0000 0000 0100 = Battery charge status: Critical
0000 0000 0000 1000 = Battery charge status: Charging
0000 0000 1000 0000 = Battery charge status: No system battery
0000 0000 1111 1111 = Battery charge status: Unknown Status

Operadores a nivel bit (https://secure.wikimedia.org/wikipedia/es/wiki/Operador_a_nivel_de_bits)

es decir que si te da 9 es decir

0000 0000 0000 1001 = 9

es por esto:

0000 0000 0000 0001 = Battery charge status: High
0000 0000 0000 1000 = Battery charge status: Charging

Código
  1.  
  2. Private Type SYSTEM_POWER_STATUS
  3.        ACLineStatus As Byte
  4.        BatteryFlag As Byte
  5.        BatteryLifePercent As Byte
  6.        Reserved1 As Byte
  7.        BatteryLifeTime As Long
  8.        BatteryFullLifeTime As Long
  9. End Type
  10. Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
  11. Private Sub Form_Paint()
  12.    'KPD-Team 2000
  13.    'URL: http://www.allapi.net/
  14.    'E-Mail: KPDTeam@Hotmail.com
  15.    Dim SPS As SYSTEM_POWER_STATUS
  16.    'get the battery powerstatus
  17.    GetSystemPowerStatus SPS
  18.    Me.AutoRedraw = True
  19.    'show some information
  20.    Select Case SPS.ACLineStatus
  21.        Case 0
  22.            Me.Print "AC power status: Offline"
  23.        Case 1
  24.            Me.Print "AC power status: OnLine"
  25.        Case 2
  26.            Me.Print "AC power status: Unknown"
  27.    End Select
  28.  
  29.    ' Ya que se tratan de flags, se omiten los else a cada if ... then
  30.    If (SPS.BatteryFlag And &H1) = &H1 Then Me.Print "Battery charge status: High"
  31.    If (SPS.BatteryFlag And &H2) = &H2 Then Me.Print "Battery charge status: Low"
  32.    If (SPS.BatteryFlag And &H4) = &H4 Then Me.Print "Battery charge status: Critical"
  33.    If (SPS.BatteryFlag And &H8) = &H8 Then Me.Print "Battery charge status: Charging"
  34.    If (SPS.BatteryFlag And &H80) = &H80 Then Me.Print "Battery charge status: No system battery"
  35.    If (SPS.BatteryFlag And &HFF) = &HFF Then Me.Print "Battery charge status: Unknown Status"
  36. End Sub
  37.  
  38.  

Dulces Lunas!ˇ.