PowerMonitor.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:2k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- using namespace System;
- using namespace System::Runtime::InteropServices;
- [StructLayoutAttribute(LayoutKind::Sequential)]
- __gc class PStat {
- public:
- System::Byte ACLineStatus;
- System::Byte BatteryFlag;
- System::Byte BatteryLifePercent;
- System::Byte Reserved1;
- System::UInt32 BatteryLifeTime;
- System::UInt32 BatteryFullLifeTime;
- };
- // Define the BOOL type
- typedef int BOOL;
- // Prototype for the function
- [DllImportAttribute("Kernel32.dll", CharSet=CharSet::Auto)]
- BOOL GetSystemPowerStatus(PStat* ps);
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(void)
- #endif
- {
- Console::WriteLine(S"Power Status Test...");
- PStat* ps = new PStat();
- BOOL b = GetSystemPowerStatus(ps);
- Console::WriteLine(S"Got status, return was {0}", __box(b));
- // Report on the AC line status
- Console::Write(S"AC line power status is ");
- switch(ps->ACLineStatus) {
- case 0:
- Console::WriteLine("'off'");
- break;
- case 1:
- Console::WriteLine("'on'");
- break;
- case 255:
- Console::WriteLine("'unknown'");
- break;
- }
- // Report on the battery status
- Console::Write(S"Battery charge status is ({0})",
- __box(ps->BatteryFlag));
- if (ps->BatteryFlag & 1)
- Console::Write(" 'high'");
- if (ps->BatteryFlag & 2)
- Console::Write(" 'low'");
- if (ps->BatteryFlag & 4)
- Console::Write(" 'critical'");
- if (ps->BatteryFlag & 8)
- Console::Write(" 'charging'");
- if (ps->BatteryFlag & 128)
- Console::Write(" 'no system battery'");
- Console::WriteLine();
- // What's the percentage charge left in the battery?
- Console::WriteLine(S"Battery life is {0}%",
- __box(ps->BatteryLifePercent));
- // How many seconds battery life is left?
- if (ps->BatteryLifeTime == -1)
- Console::WriteLine("Battery life in seconds: Unknown");
- else
- Console::WriteLine(S"Battery seconds remaining: {0} secs",
- __box(ps->BatteryLifeTime));
- return 0;
- }