static extern bool DeviceIoControl( Microsoft.Win32.SafeHandles.SafeFileHandle hDevice, uint IoControlCode, ref BATTERY_WAIT_STATUS InBuffer, int nInBufferSize, ref BATTERY_STATUS OutBuffer, int nOutBufferSize, ref int pBytesReturned, IntPtr Overlapped );
public override void UpdateValues() { batterystats = Getbatterystatus(batterypath); ForegroundMeter.Percent = batterystats.Capacity / 42400.0; if (batterystats.Rate > 0) { BackgroundMeter.Percent = Clamp((batterystats.Rate - 29000) / 4000.0); BackgroundMeter.ColorEnd = Color.FromArgb(0, 178, 148); } else if (batterystats.Rate == 0) { BackgroundMeter.Percent = 0.0; } else { BackgroundMeter.Percent = Clamp((-batterystats.Rate - 2000) / 10000.0); BackgroundMeter.ColorEnd = Color.FromArgb(255, 178, 148); } HighlightMeter.Percent = 0.0; //Debug.WriteLine(batterystats.Capacity); }
private BATTERY_STATUS Getbatterystatus(string path) { BATTERY_WAIT_STATUS bws; BATTERY_STATUS res = new BATTERY_STATUS(); SafeFileHandle hBattery; int err = 0, outLen = 0; int wait = 1000, s = 0; hBattery = CreateFile(path, 0x80000000 | 0x40000000, 0x1 | 0x2, IntPtr.Zero, 3, 0x80, IntPtr.Zero); err = Marshal.GetLastWin32Error(); DeviceIoControl(hBattery, 2703424, ref wait, Marshal.SizeOf(wait), ref s, Marshal.SizeOf(s), ref outLen, IntPtr.Zero); err = Marshal.GetLastWin32Error(); bws.BatteryTag = s; bws.Timeout = 10000; bws.PowerState = 0x1 | 0x2 | 0x4 | 0x8; bws.LowCapacity = 0; bws.HighCapacity = 10000; DeviceIoControl(hBattery, 2703436, ref bws, Marshal.SizeOf(bws), ref res, Marshal.SizeOf(res), ref outLen, IntPtr.Zero); err = Marshal.GetLastWin32Error(); //MessageBox.Show(res.Capacity.ToString()); hBattery.Close(); return(res); }