CIM_Battery WMI Class has no instances

0

I am trying to create a C# Application that records the battery percentage over time, and to do that I have been trying to use the .NET WMI API. I looked on MSDN and found that there is a WMI class called CIM_Battery, and confirmed its existence with wbemtest.exe.

Upon closer inspection in wbemtest.exe, it appears that the CIM_Battery class doesn't have any instances that I can read from.

Screenshot of CIM_Battery Class

Is this normal, or is my system just broken?

ifconfig

Posted 2018-03-23T22:26:11.643

Reputation: 298

So your computer has a battery? – Ultrasonic54321 – 2018-03-23T22:27:57.563

@Ultrasonic54321 Yes, it does. – ifconfig – 2018-03-24T03:25:08.160

Answers

1

Not sure why that isn't working, but another way to do it:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C powercfg /batteryreport";
process.StartInfo = startInfo;
process.Start();

This will output the battery life to a file: C:/Windows/System32/battery-report.html

You can then scrape that page. It's a bit overkill, but there might be more data in there that you can use for your diagnostics.

Edit: also a powershell class in .net (System.Management.Automation) assembly. So you could make a .ps1 with the line: (Get-WmiObject win32_battery).estimatedChargeRemaining

PowerShell psinstance = PowerShell.Create();
psinstance.AddScript(scriptPath);
var results = psinstance.Invoke();

Edit2: The CIM (common information model) classes are parent classes upon which the WMI classes are built. win32_xxx classes monitor and manage system hardware and features and are located in in CIM. Instead of pulling from CIM_Battery parent class, pull from the win32_battery class instead.

Narzard

Posted 2018-03-23T22:26:11.643

Reputation: 2 276

Is this really the only way to do it? It just seems... unclean. – ifconfig – 2018-03-24T06:04:50.500

there's also the powershell command (Get-WmiObject win32_battery).estimatedChargeRemaining and returns the Percentage left. Might be easier? There is a powershell class in .net so you can grab the output pretty easy prob – Narzard – 2018-03-24T06:08:51.540

Wouldn't the PowerShell call to WMI return nothing because there are no instances? – ifconfig – 2018-03-24T07:30:27.870

1Open a powershell window and paste the command and see if you get a result – Narzard – 2018-03-24T15:15:09.423

Yes, the PS Command does work. OH, I think I figured it out: are the CIM_[WHATEVER] things just the classes and the other ones are the instances? Let me try the win32_battery class. – ifconfig – 2018-03-24T17:31:46.840

THANK YOU! I will accept your answer if you include what I said in my previous comment: the CIM_Battery class is just a template for the win32_battery class which implements CIM_Battery. That is why CIM_Battery has no instances, win32_battery does. – ifconfig – 2018-03-24T17:35:22.633

1@ifconfig added more info in the 2nd edit. Glad you figured it out! – Narzard – 2018-03-24T17:45:17.313