zoukankan      html  css  js  c++  java
  • 整理出来的一个windows关机、锁定、重启、注销 API调用

    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. }  
  • 相关阅读:
    柱状图 highcharts 柱状图默认是显示的 Heighcharts.com 的版权。设置去掉不显示(非商业)
    eclipse下使用maven配置库托管jar包
    Java的云打印Lodop
    文本框限制输入类型<input>的输入框
    初次使用JFinal
    【原创】java实现两单链表相加求和
    【原创】Springboot的Filter拦截器中使用@value获取值为null
    【原创】Oracle主从同步---创建物理备份数据库[Creating a Physical Standby Database]
    【原创】基于Telnet协议的Jenkins远程部署
    【原创】FastDFS简单安装配置-----同一台机器测试
  • 原文地址:https://www.cnblogs.com/alan666/p/8312186.html
Copyright © 2011-2022 走看看