1

I am trying to run the command powercfg /srumutil /output C:\\EnergyConsumption\\srumutil.csv /csv through a C# application. The command above runs successfully in powershell and cmd but it returns the following error when executed through my C# program:

Unable to perform operation. An unexpected error (0x1f) has occured: A device attached to the system is not functioning.

I will clarify that:

  • I am running this command on a machine with battery (laptop) in both cases.
  • Visual studio is running as administrator and process is started with Verb = "runas" so it should be elevated as well.
  • I have tried every variation of this in C# including (all these failed):
    • cmd.exe /K powercfg /srumutil
    • powershell.exe Start-Process powercfg /srumutil
    • putting it in a bat file and running the bat file with Process.Start().
  • Other arguments will work with Process.Start and powercfg like powercfg /L but never /srumutil

The code in question:

using System;
using System.Diagnostics;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var info = new ProcessStartInfo()
            {
                FileName = "powercfg.exe",
                Arguments = "/srumutil /output C:\\EnergyConsumption\\srumutil.csv /csv",
                Verb = "runas",
                UseShellExecute = false
            };

            var proc = Process.Start(info);
            proc.WaitForExit();
            Console.ReadLine();
        }
    }
}

Long story short powercfg /srumutil works as intended everywhere except through Process.Start() function. Is there something I am missing here?

JimS
  • 349
  • 1
  • 4
  • 19
  • Do the other methods work _on the same machine_? I just tried in a VM, and I get the error even from an elevated PowerShell session. Note that, because of `UseShellExecute = false`, `Verb = "runas"` is _ignored_ in your code. – mklement0 Mar 24 '22 at 21:40
  • @mklement0 yes all powercfg commands including srumutil work on the same machine through powershell or cmd but only srumutil fails through Process.Start() – JimS Mar 25 '22 at 15:05
  • @mklement0 Ok, I was confused as to why it would fail in powershell for you but after reading the accepted answer it makes sense if you ran it in x86. – JimS Mar 25 '22 at 15:15
  • Certainly good to know that it doesn't work from 32-bit processes, but I was actually running from a 64-bit one. I _suspect_ that the problem in my case is related to running in a _VM_ - there may be no (virtual) device present that provides this data. – mklement0 Mar 25 '22 at 15:24

1 Answers1

2

I think your code is fine and the issue is in the execution context as you suspected. I get the same error when trying to run powercfg /srumutil in non-elevated PowerShell session, while it works fine in elevated which points into lack of admin rights. But, in this case, the error message might be misleading. I also get the same error when running the code in x86 process, instead of x64 - both in PowerShell and C#. Try to check if your application is x86 or x64 and compare that to the PowerShell process in which you test that powercfg /srumutil runs fine.

WiktorM
  • 61
  • 2
  • Confirmed your hypothesis. I changed the configuration manager to use x64 when building in visual studio and it works whereas x86 does not. So apparently, powercfg /srumutil only works on x64 processes? I would assume this is a really important restriction and would be mentioned in the docs : https://learn.microsoft.com/en-us/windows-hardware/design/device-experiences/powercfg-command-line-options#option_srumutil – JimS Mar 25 '22 at 15:12