整理出来的一个windows关机、锁定、重启、注销 API调用

来源:互联网 发布:java调用浏览器下载 编辑:程序博客网 时间:2024/05/17 22:19
  1. using System.Runtime.InteropServices;  
  2.   
  3. namespace HookDemo  
  4. {  
  5.     class WindowsExit  
  6.     {  
  7.         [StructLayout(LayoutKind.Sequential, Pack = 1)]  
  8.         private struct TokPriv1Luid  
  9.         {  
  10.             public int Count;  
  11.             public long Luid;  
  12.             public int Attr;  
  13.         }  
  14.         [DllImport("kernel32.dll", ExactSpelling = true)]  
  15.         private static extern IntPtr GetCurrentProcess();  
  16.   
  17.         [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]  
  18.         private static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);  
  19.   
  20.         [DllImport("advapi32.dll", SetLastError = true)]  
  21.         private static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);  
  22.   
  23.         [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]  
  24.         private static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,  
  25.             ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);  
  26.   
  27.         [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]  
  28.         private static extern bool ExitWindowsEx(int DoFlag, int rea);  
  29.         [DllImport("user32.dll")]  
  30.         public static extern void LockWorkStation();  
  31.   
  32.         private const int SE_PRIVILEGE_ENABLED = 0x00000002;  
  33.         private const int TOKEN_QUERY = 0x00000008;  
  34.         private const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;  
  35.         private const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";  
  36.         private const int EWX_LOGOFF = 0x00000000;  
  37.         private const int EWX_SHUTDOWN = 0x00000001;  
  38.         private const int EWX_REBOOT = 0x00000002;  
  39.         private const int EWX_FORCE = 0x00000004;  
  40.         private const int EWX_POWEROFF = 0x00000008;  
  41.         private const int EWX_FORCEIFHUNG = 0x00000010;  
  42.   
  43.         private static bool DoExitWin(int DoFlag)  
  44.         {  
  45.             bool ok;  
  46.             TokPriv1Luid tp;  
  47.             IntPtr hproc = GetCurrentProcess();  
  48.             IntPtr htok = IntPtr.Zero;  
  49.             ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);  
  50.             tp.Count = 1;  
  51.             tp.Luid = 0;  
  52.             tp.Attr = SE_PRIVILEGE_ENABLED;  
  53.             ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);  
  54.             ok = AdjustTokenPrivileges(htok, falseref tp, 0, IntPtr.Zero, IntPtr.Zero);  
  55.             ok = ExitWindowsEx(DoFlag, 0);  
  56.             return ok;  
  57.         }  
  58.   
  59.         /// <summary>  
  60.         /// 锁定计算机  
  61.         /// </summary>  
  62.         public static void Lock()  
  63.         {  
  64.             LockWorkStation();  
  65.         }  
  66.   
  67.         /**/  
  68.         /// <summary>  
  69.         /// 重新启动  
  70.         /// </summary>  
  71.         public static bool Reboot()  
  72.         {  
  73.             return DoExitWin(EWX_FORCE | EWX_REBOOT);  
  74.         }  
  75.   
  76.         /**/  
  77.         /// <summary>  
  78.         /// 关机  
  79.         /// </summary>  
  80.         public static bool PowerOff()  
  81.         {  
  82.             return DoExitWin(EWX_FORCE | EWX_POWEROFF);  
  83.         }  
  84.   
  85.         /**/  
  86.         /// <summary>  
  87.         /// 注销  
  88.         /// </summary>  
  89.         public static bool LogOff()  
  90.         {  
  91.             return DoExitWin(EWX_FORCE | EWX_LOGOFF);  
  92.         }  
  93.     }  
  94. }  
0 0
原创粉丝点击