Ryan Phung
Ryan Phung

Reputation: 181

Error "The Power Efficiency Diagnostic library (energy.dll) could not be loaded" when running powercfg.exe -batteryreport from .NET

I think this is not a duplicate question of How to start `powercfg.exe -energy` from a .NET app?

In the other question, the OP has accepted an answer but the issue just resolved on its own so we don't know what is the solution (I'm not entirely sure, does this mean it's okay for me to ask about it in this question - since the solution in the other question likely won't apply to my case?)


Question:

I'm trying to read battery information (specially Design Capacity and Full Charge Capacity) from .NET.

When I run powercfg.exe -batteryreport -xml from the command line, it outputs a battery report as an xml file. I figure out that I could run this and then read the xml file. So I wrote the below codes:

        var process = new Process();
        //process.StartInfo.WorkingDirectory = "C:\\Windows\\System32";
        process.StartInfo.FileName = "powercfg";
        process.StartInfo.Arguments = "-batteryreport -xml";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.Start();

        Console.Write(process.StandardOutput.ReadToEnd());
        Console.Write(process.StandardError.ReadToEnd());

However it gives me this error:

The Power Efficiency Diagnostic library (energy.dll) could not be loaded

If anyone has a suggestion please enlighten me - or even if you could point me in a direction that will allow me to troubleshoot it, I would really appreciate. Thank you.


Update 1: I just tried the above same code on another laptop with a different configuration and it works!

Laptop 1: HP, Windows 8.1, .NET Framework 4.5.1 - DO NOT WORK Laptop 2: Macbook, Windows 7, .NET Framework 4.5 - WORK

Seem I have to find out the difference between the 2 laptops.


Update 2: compiling the code on laptop 2 and brings the .exe over to laptop 1 seem to work as well!


Update 3: I think I've found out what is the problem.

By default, when I create a project in Visual Studio, the project build settings has this flag "Prefer 32-bit". When this runs on a 64-bit windows it cause a problem since I guess the .dll is 64 bit.

Simply unchecking it solved the problem for me - wow!

I don't have enough "reputation" to post image, so here you go, for those who have the same problem:

Upvotes: 1

Views: 2865

Answers (0)

Related Questions