GetSystemPowerStatus dll import

 

Hey

I would like import the kernell32.dll function GetSystemPowerStatus to know if I am running on UPS in case of a power loss.

The problem is that the function receives a pointer to a struct to write the output to:

BOOL WINAPI GetSystemPowerStatus( _Out_  LPSYSTEM_POWER_STATUS lpSystemPowerStatus ); 

typedef struct _SYSTEM_POWER_STATUS {
  BYTE  ACLineStatus;
  BYTE  BatteryFlag;
  BYTE  BatteryLifePercent;
  BYTE  Reserved1;
  DWORD BatteryLifeTime;
  DWORD BatteryFullLifeTime; 
} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS; 


What is the correct way to import and use this kind of function in metatrader?

Thanks. 

 
fudge:

Hey

I would like import the kernell32.dll function GetSystemPowerStatus to know if I am running on UPS in case of a power loss.

The problem is that the function receives a pointer to a struct to write the output to:


What is the correct way to import and use this kind of function in metatrader?

Thanks. 

Some part of this article show how to import a structure in file operation https://www.mql5.com/en/articles/1543.

BTW, if you want to get free help for some WinAPI programming, you should also give a link to msdn, so the person who answer you don't have to go through msdn just to help you.

GetSystemPowerStatus function http://msdn.microsoft.com/en-us/library/windows/desktop/aa372693(v=vs.85).aspx

SystemPowerStatus structure http://msdn.microsoft.com/en-us/library/windows/desktop/aa373232(v=vs.85).aspx 

AFAIK, GetSystemPowerStatus is for notebook with battery, not for PC with UPS.

 

fudge:

I would like import the kernell32.dll function GetSystemPowerStatus to know if I am running on UPS in case of a power loss.

What is the correct way to import and use this kind of function in metatrader?
  1. GetSystemPowerStatus is for notebook with battery, not for PC with UPS. On a UPS the PC doesn't see a power glitch.
  2. If the power is out, do you still have a Internet connection? I don't mean unplugging the PC. I mean, the up stream ISP's routers will be down also depending on the size of the outage.
  3. I personally don't worry about those things. The SL is in place. If I'm not using a real TP, I implement a leading TP (opposite of a trailing SL) so if I loose connection the order will be closed.
  4. Here's the code based on my quick read:
Not compiled Not tested.
/*
 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa373232%28v=vs.85%29.aspx
 * BOOL WINAPI GetSystemPowerStatus( _Out_ LPSYSTEM_POWER_STATUS lpSystemPowerStatus ); 
 *
 *  typedef struct _SYSTEM_POWER_STATUS {
 *    BYTE  ACLineStatus;                       [0]
 *    BYTE  BatteryFlag;
 *    BYTE  BatteryLifePercent;
 *    BYTE  Reserved1;
 *    DWORD BatteryLifeTime;                    [1]
 *    DWORD BatteryFullLifeTime;                [2]
 *  } SYSTEM_POWER_STATUS
 */
#import "kernel32.dll"
bool GetSystemPowerStatus(int& SPSinfoArray[]);
#import
int SPSinfoArray[3];
bool GetSPSinfo(){  return( GetSystemPowerStatus(SPSinfoArray) );   }
                                                    #define ACLS_OFFLINE      0
                                                    #define ACLS_ONLINE       1
                                                    #define ACLS_UNKNOWN    255
int ACLineStatus(){         return( SPSinfoArray[0]&0xF         );  }
                                                    #define BF_NOTCHARGING    0
                                                    #define BF_66PCT          1
                                                    #define BF_33PCT          2
                                                    #define BF_05PCT          4
                                                    #define BF_CHARGING       8
                                                    #define BF_NOBATTERY    128
                                                    #define BF_UNKNOWN      255
int BatteryFlag(){          return((SPSinfoArray[0]&0xF0 ) >> 8 );  }
                                                    #define BLP_UNKNOWN     255
int BatteryLifePercent(){   return((SPSinfoArray[0]&0xF00) >> 16);  }
                                                    #define BLT_UNKNOWN     -1
int BatteryLifeTime(){      return( SPSinfoArray[1] );  } // Seconds remaining.
                                                    #define BFLT_UNKNOWN    -1
int BatteryFullLifeTime(){  return( SPSinfoArray[2] );  } // Seconds when Full.
Not compiled Not tested.
 

Thanks for the quick response.
Next time I will post the direct links to the MSDN documentation.
My UPS is connected via USB cable and treated by windows as a "laptop battery" :), so this should work for me very well. 

P.S. 

The router is connected to the UPS as well.

 

Here is the corrected code. There was a mistake in the MASK.
BTW, I am getting the value 9 for BatteryFlag(). Didn't find any documentation about it... 


/*
 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa373232%28v=vs.85%29.aspx
 * BOOL WINAPI GetSystemPowerStatus( _Out_ LPSYSTEM_POWER_STATUS lpSystemPowerStatus ); 
 *
 *  typedef struct _SYSTEM_POWER_STATUS {
 *    BYTE  ACLineStatus;                       [0]
 *    BYTE  BatteryFlag;
 *    BYTE  BatteryLifePercent;
 *    BYTE  Reserved1;
 *    DWORD BatteryLifeTime;                    [1]
 *    DWORD BatteryFullLifeTime;                [2]
 *  } SYSTEM_POWER_STATUS
 */
#import "kernel32.dll"
bool GetSystemPowerStatus(int& SPSinfoArray[]);
#import
int SPSinfoArray[3];
bool GetSPSinfo(){  return( GetSystemPowerStatus(SPSinfoArray) );   }
                                                    #define ACLS_OFFLINE      0
                                                    #define ACLS_ONLINE       1
                                                    #define ACLS_UNKNOWN    255
int ACLineStatus(){         return( SPSinfoArray[0]&0xFF         );  }
                                                    #define BF_NOTCHARGING    0
                                                    #define BF_66PCT          1
                                                    #define BF_33PCT          2
                                                    #define BF_05PCT          4
                                                    #define BF_CHARGING       8
                                                    #define BF_NOBATTERY    128
                                                    #define BF_UNKNOWN      255
int BatteryFlag(){          return((SPSinfoArray[0]&0xFF00 ) >> 8 );  }
                                                    #define BLP_UNKNOWN     255
int BatteryLifePercent(){   return((SPSinfoArray[0]&0xFF0000) >> 16);  }
                                                    #define BLT_UNKNOWN     -1
int BatteryLifeTime(){      return( SPSinfoArray[1] );  } // Seconds remaining.
                                                    #define BFLT_UNKNOWN    -1
int BatteryFullLifeTime(){  return( SPSinfoArray[2] );  } // Seconds when Full.
 
fudge:

Here is the corrected code. There was a mistake in the MASK.
BTW, I am getting the value 9 for BatteryFlag(). Didn't find any documentation about it...

  1. Byte: 0xFF. Needed more coffee
  2. 9 = 1+8 BF_66PCT and BF_CHARGING
 
9 = 1+8 BF_66PCT and BF_CHARGING
Yep, already figured that one out :)