1

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?

Community
  • 1
  • 1
Alex
  • 689
  • 1
  • 8
  • 22
  • 1
    Why do you want to do that? What is the real problem you are trying to solve? Having an application initiate a reboot without warning is not the most friendly behavior. There may other ways to do what you want, or it may be acceptable to schedule a reboot in advance with user consent – Panagiotis Kanavos Feb 05 '15 at 11:58
  • @Panagiotis Funny, I had just edited the question to remove this info. I'm adding a function to my existing program which runs on login for multiple machines on our network. At the end of the day, the machines need to be logged out (not locked). Sometimes they are just locked (not logged out). I can see this because my program is still running. The idea is I can restart the machines that are locked. – Alex Feb 05 '15 at 12:19
  • And why would you want to do that instead of eg shutting down? Anyway, you don't need a local program to shutdown/reboot. You can schedule the command on the user's machines to run eg every day at 2am. You can deploy the scheduled task to all computers using a Group Policy. – Panagiotis Kanavos Feb 05 '15 at 12:26
  • Restarting vs shutting down because i need them available for the morning. The problem is, there are hundreds of these machines on my network and adding scheduled tasks will be cumbersome. In any case, my question is whether or not theres another way to do this in c#. Im aware that there are other ways to restart a computer. – Alex Feb 05 '15 at 12:30
  • That's why it's preferable to use a Group Policy for such things. You define/modify the scheduled task in the policy at *Computer Configuration > Preferences > Control Panel Settings > Scheduled tasks*. The task will be deployed to all the machines you want. Pushing a program to all machines is more cumbersome, especially if you want to change the reboot time at some point. – Panagiotis Kanavos Feb 05 '15 at 12:38
  • Thanks for the advice, but the program is already pushed because it serves several other purposes. Also, changing times isn't a problem, my constants are on a server. Furthermore, this advice doesn't answer my question. – Alex Feb 05 '15 at 13:39

1 Answers1

2

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

Alex
  • 689
  • 1
  • 8
  • 22