[Code Snippet]How to shut down the computer on C#
2005-11-07 09:54
573 查看
[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 ); 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 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 ); }
相关文章推荐
- (C#) A demo on how to auto fill out the "account" value in log in web page in "WebBrowser" control.
- PHPFarm - How to run multiple versions of PHP on the same computer
- How to populate the datagrid on background thread with data binding by using Visual C#
- [Selenium]How to click on a hidden link ,move to the drop down menu and click submenu
- iOS 如何在苹果电脑上安装svn 服务器(how to install svn server on the computer of mac os x)
- How to change the computer name on Ubuntu
- How_to_build_an_ARM/DSP_Hello_World_program_on_the_DaVinci_EVM 续
- The Visual Studio Remote Debugger service on the target computer cannot connect back to this computer
- How To Add Simple "Install WebPart" Menu to the Right Click On a CAB File
- How to create universal static libraries on Xcode 4: the traditional wa
- [English Homwork] How to make money on the Internet
- How To Verify the Word Size (32bit vs 64bit) of the Oracle Binary on MS Windows systems (文档 ID 46506
- How to add dependency on a Windows Service AFTER the service is installed
- How to fix the bug 640*480 resolution only when install ubuntu 14.04 on a old laptop?
- HOW TO RUN YOUR WEB SERVER AND MQTT WEBSOCKETS BROKER ON THE SAME PORT
- How to enable single sign-on service on the SPS
- How to install the latest Nvidia drivers on Ubuntu 14.04 Trusty Tahr
- How to install the Sun Java JDK on Ubuntu 10.10
- How To Get The Size of RAW Devices On Linux
- How to safely shut down a loading UIWebView in viewWillDisappear?