4

When running bcdedit.exe from an elevated command prompt, you can see the values of the current BCD settings. I need to read the setting/value of hypervisorlaunchtype.

Screenshot

Does anyone know a way to do this?

I've tried to write the piped output to a tmp file so that I could parse it, but ran into piped output issues due to the fact that bcdedit.exe needs to be run from an elevated prompt. Maybe there's a better way?

Edit: I forgot to add that I'd like to be able to do this without the end user seeing the Command Prompt at all (i.e. not even a quick flash) is possible.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94

1 Answers1

2

First, run your Visual Studio as Administrator and try this code in a console application (run the application with debugging):

    static void Main(string[] args)
    {

        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.FileName = @"CMD.EXE";
        p.StartInfo.Arguments = @"/C bcdedit";
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        // parse the output
        var lines = output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Where(l => l.Length > 24);
        foreach (var line in lines)
        {
            var key = line.Substring(0, 24).Replace(" ", string.Empty);
            var value = line.Substring(24).Replace(" ", string.Empty);
            Console.WriteLine(key + ":" + value);
        }
        Console.ReadLine();
    }

However, there is a problem, If you want this to work when launching the application from outside the elevated Visual Studio, you need to configure your application to ask for elevated rights:

On your project, click add new item and select Application Manifest File.

Open app.manifest file and replace this line:

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

with this one:

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
Cosmin Vană
  • 1,562
  • 12
  • 28
  • Thanks, this works great, but it flashes the command prompt while reading the info from BCDedit.exe. I forgot to mention that I'd like it to remain hidden from the user as I'm making a GUI of sorts for it, and always flashing the command window is icky. =) I updated my question to reflect this. It seems to be an issue with the use of `UseShellExecute`'s value. Any idea on a workaround? – J. Scott Elblein Jan 12 '14 at 23:50
  • 1
    @J.ScottElblein have you tried using `ProcessStartInfo` and setting the `CreateNoWindow = true`? – Pete Garafano Jan 12 '14 at 23:53
  • 1
    You can also try this: p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; – Cosmin Vană Jan 13 '14 at 09:26