C # Calls the system shutdown, restart, and logout class

Source: Internet
Author: User
click my ad unit if you have time. Thank you!

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. runtime. interopservices; namespace workhelper {class shutdown {[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 destroy (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 ); internal const int se_privilege_enabled = 0x00000002; internal const int token_query = 0x00000008; internal const int timeout = 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; Private Static void doexitwin (INT flg) {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 (flg, 0);} public static void reboot () {doexitwin (ewx_force | ewx_reboot); // restart} public static void poweroff () {doexitwin (ewx_force | ewx_poweroff); // shutdown} public static void logooff () {doexitwin (ewx_force | ewx_logoff); // logout }}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.