Search code examples
c#.netsystem-shutdown

Reboot machine from C#/Forms App while session is locked


Referring to this question: .net - Reboot machine from a C#/WPF App

I am attempting to create a c#/.net app that can restart the machine even if the session is locked (i.e., user is logged in, this app is running, but session is locked).

I tried this from the question: System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0");

but apparently that only works if the session is unlocked. Additionally, after reading this: MSDN - InitiateSystemShutdown Function it seems the InitiateSystemShutdown function will display the System Shutdown dialog box, which doesn't seem like it will suite my purposes.

Are there any other methods of doing this?


Solution

  • The ExitWindowsEx function accomplished what I was trying to do.

    Using:

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
    

    after adjusting token privileges, and using uFlags 0x06 (reboot / force). I used dwReason 0 as well. This function will restart the machine whether or not the session is locked.

    here