Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hi guys. I really could use some help with a program i'm writing about battery life. I want to list some of a laptop's battery properties, such as design capacity, design voltage, current discharge rate and others, but i cannot find this info using the WIn32_Battery or Win32_PortableBattery classes...the values of most of these classes' properties are null. In the msdn documentation i found some structs but i cannot access them in CSharp and .net (they're designed for windows and c/c++). Does anyone know how to find this info using CSharp and .net?
Posted

Did you tried with

C#
[DllImport("kernel32.dll")]
static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS
   lpSystemPowerStatus)



Actually I never tried it, but this should help.

http://www.pinvoke.net/default.aspx/kernel32/GetSystemPowerStatus.html[^]


Let us know!

Cheers


UPDATE:

I just checked a simple solution:

C#
using System;
using System.Runtime.InteropServices;

public class SystemPower
{
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern Boolean GetSystemPowerStatus(out SystemPowerStatus sps);

    private enum ACLineStatus : byte
    {
        Offline = 0,
        Online = 1,
        Unknown = 255
    }

    private enum BatteryFlag : byte
    {
        High = 1,
        Low = 2,
        Critical = 4,
        Charging = 8,
        NoSystemBattery = 128,
        Unknown = 255
    }

    private struct SystemPowerStatus
    {
        public ACLineStatus LineStatus;
        public BatteryFlag flgBattery;
        public Byte BatteryLifePercent;
        public Byte Reserved1;
        public Int32 BatteryLifeTime;
        public Int32 BatteryFullLifeTime;
    }

    /// <summary>
    /// Returns true if a an AC Power Line is detected
    /// </summary>
    public static Boolean ACPowerPluggedIn()
    {
        SystemPowerStatus SPS = new SystemPowerStatus();
        GetSystemPowerStatus(out SPS);

        if (SPS.LineStatus == ACLineStatus.Online)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Returns an integer representing the percent of battery charge remaining.
    /// </summary>
    public static Int32 BatteryCharge()
    {
        SystemPowerStatus SPS = new SystemPowerStatus();
        GetSystemPowerStatus(out SPS);

        return (Int32)SPS.BatteryLifePercent;
    }
}


C#
private void btnCheckBattCharge_Click(object sender, EventArgs e)
{
    lblBatteryCharge.Text = SystemPower.BatteryCharge().ToString();
}


And is working like a charm!

So, based on this example you can retrieve all information you need.

Cheers again!
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Oct-11 17:04pm    
Good starting point. pinvoke.net is very helpful for such things. My 5.
--SA
Nickso112 7-Oct-11 17:37pm    
I have tried to remove my mistake..i'm at work and i can only use my phone for this forum..

The problem is this: i need the information from the BATTERY_INFORMATION struct (designed capacity and full charged capacity) and from the BATTERY_STATUS struct (charge rate). I found a posible sollution for the BATTERY_STATUS struct, using the function DeviceIoControl(out BATTERY_STATUS), possible because i'm at work and i've tried it only on a desktop with no batteries, but now i need a function, simmilar to the DeviceIoControl and GetSystemPowerStatus functions, to return the properties of a BATTERY_INFORMATION object.

Thank you for your interest and patience...
Dave Kreskowiak 7-Oct-11 21:59pm    
BTW, there is nothing that says that a hardware vendor has to supply this information to windows. You may be expending all of this effort for nothing.
Nickso112 7-Oct-11 23:10pm    
Hwinfo64 gives me this info...i just need a way to get it in managed code...
Nickso112 8-Oct-11 4:37am    
Well...i found a way to get three of the four parameters i was looking for...but i cannot post an answer...i deleted one yesterday and now i can only change the deleted one, which no one else can see...can a moderator help me show the sollution i found?
You could use
System.Windows.Forms.PowerStatus
 
Share this answer
 
Comments
Dave Kreskowiak 4-Aug-14 15:49pm    
Do NOT reply to 3 year old questions!

Besides, internally, the .NET Framework uses code that is not very far off from what the OP ended up using.
Sepehr Mohammad 4-Aug-14 16:04pm    
Well, I thought someone else might find this question on Google as I did.
That's true but don't you think that using .NET classes is better than writing a wrapper yourself? There are many things you might not consider ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900