How do I turn my computer off, restart, sleep programmatically?

Asked

Viewed 2,862 times

7

I would like to add these options in a program, after finishing a task the computer would be shut down/restart/put on Sleep.

How can I do that?

2 answers

8


To manage operations as is I created a class to call the methods of external Dlls:

public class WinPower
{
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct TokPriv1Luid
    {
        public int Count;
        public long Luid;
        public int Attr;
    }

    [DllImport("kernel32.dll", ExactSpelling = true)]
    internal static extern IntPtr GetCurrentProcess();

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
    phtok);

    [DllImport("advapi32.dll", SetLastError = true)]
    internal static extern bool LookupPrivilegeValue(string host, string name,
    ref long pluid);

    [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

    [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool ExitWindowsEx(int flg, int rea);

    [DllImport("Powrprof.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);


    internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
    internal const int TOKEN_QUERY = 0x00000008;
    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";

    internal const int EWX_LOGOFF = 0x00000000;
    internal const int EWX_SHUTDOWN = 0x00000001;
    internal const int EWX_REBOOT = 0x00000002;
    internal const int EWX_FORCE = 0x00000004;
    internal const int EWX_POWEROFF = 0x00000008;
    internal const int EWX_FORCEIFHUNG = 0x00000010;

    public enum PowerOption
    {
        //Logoff = 0,
        Reboot = 2,
        PowerOff = 8,
        PowerOffForce = 4,
        PowerOffForceIfHung = 10,

        Sleep = 100,
        Hibernate = 102
    }

    public static void Execute(PowerOption option)
    {
        if (option == PowerOption.Sleep)
        {
            SetSuspendState(false, true, true);
            return;
        }
        else if (option == PowerOption.Hibernate)
        {
            SetSuspendState(true, true, true);
            return;
        }
        else
        {
            bool ok;
            TokPriv1Luid tp;
            IntPtr hproc = GetCurrentProcess();
            IntPtr htok = IntPtr.Zero;
            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
            tp.Count = 1;
            tp.Luid = 0;
            tp.Attr = SE_PRIVILEGE_ENABLED;
            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
            ok = ExitWindowsEx((int)option, 0);
        }
    }
}

The method Execute is what will perform the operation, use examples:

WinPower.Execute(WinPower.PowerOption.PowerOff);
WinPower.Execute(WinPower.PowerOption.Reboot);
WinPower.Execute(WinPower.PowerOption.Sleep);
WinPower.Execute(WinPower.PowerOption.Hibernate);

If the computer sleeps or hibernates, simply invoke the method SetSuspendState

rundll32.exe powrprof.dll,SetSuspendState 0,1,1 // dormir
rundll32.exe powrprof.dll,SetSuspendState 1,1,1 // hibernar

In case of shutdown/restart it is necessary to invoke the other methods and finally invoke ExitWindowsEx passing the value (2 = restart, 8 = shutdown)

References (in English):

7

You can use the library System.Diagnostics.

Using System.Diagnostics;

Within some action you can enter the command Process.Start:

{
    Process.Start("Shutdown", "-i");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.