/// <summary> /// Method for getting information about battery from win32 structure - BATTERY_INFORMATION /// </summary> /// <returns>Dictionary with pairs - property names, property value</returns> public override Dictionary <string, string> GetBatteryInfo() { try { BATTERY_INFORMATION bi = new BATTERY_INFORMATION(); if (LibWrap_Win32_BatteryInfo.GetBatteryInfo(ref bi)) { ConvertBATT_INFO_To_Dictionary(bi); return(base.batteryInfo); } return(null); } catch (Exception e) { throw new Exception("Не удалось получить информацию от Win32 API\n" + e.Message); } }
/// <summary> /// Method for converting struct BATTERY_INFO to dictionary<string, string> /// </summary> /// <param name="bi">Struct with battery information for coverting to dictionary</param> private void ConvertBATT_INFO_To_Dictionary(BATTERY_INFORMATION bi) { Type biType = bi.GetType(); FieldInfo[] fiArr = biType.GetFields(); foreach (FieldInfo fi in fiArr) { if (fi.FieldType.IsArray) { base.InsertPairToDictionary(fi.Name, (Encoding.UTF8.GetString((byte[])fi.GetValue(bi))).TrimEnd('\0')); } else { base.InsertPairToDictionary(fi.Name, fi.FieldType == typeof(byte)?((byte)fi.GetValue(bi)).ToString():fi.GetValue(bi).ToString()); } } }
public static extern bool GetBatteryInfo(ref BATTERY_INFORMATION bi);