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

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

  1. 编程实现Windows关机、重启、注销

    要想编程使Windows关机.重启或者注销,可以使用ExWindowsEx这个API函数,该函数只有两个参数,第一个表示关机动作的标志,也就是你要让该函数关机呢,还是重启,还是注销等.可以使用EWX_ ...

  2. Android系统关机或重启的几种实现方式

    前阵子工作上遇到一些关于Android系统关机或重启的系统修改,于是,做了一些尝试,也搜集了一下资料,现在整理一下,做一些总结,方便学习或者日后工作的需要. 默认的SDK并没有提供应用开发者直接的An ...

  3. 手机控制电脑定时关机,重启WiFi

    需求 晚上上床,电脑开着WiFi让手机上网.要么上床之前就给电脑设置定时关机:要么就电脑开通宵:要么就待会下来关电脑.这3种情况都非常不好,要么麻烦,要么浪费. 最无奈的是电脑刚开好WiFi,上床后才 ...

  4. 用 C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠

    简介 本文讲述了用 C# 代码如何实现让你的电脑关机,重启,注销,锁定,休眠,睡眠. 如何实现 首先,使用 using 语句添加我们需要的命名空间: using System.Diagnostics; ...

  5. 使用c#对windows进行关机、重启或注销

    方法一:调用windows自带的shutdown.exe (缺点:会出现倒计时窗口) System.Diagnostics.Process.Start("shutdown.exe" ...

  6. C#实现控制Windows系统关机、重启和注销的方法:

    shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...

  7. C/C++控制Windows关机/注销/重启的正确姿势

    简介 说到代码控制Windows关机/注销/重启的方式,有很多种,最简单的不过就是控制命令行,使用system("pause")函数执行一个shutdown -s -t 0,关机就 ...

  8. C#实现控制Windows系统关机、重启和注销的方法

    shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...

  9. wpf实现一个windows定时关机的工具

    基本界面 起源 在家睡前喜欢用电脑放情景喜剧看,电脑需要定时关机,一开始直接命令行定时关机,感觉有点小麻烦, 于是最近弄了个有界面的 主要功能 在指定的时间之后执行 关机|休眠|重启 的操作, 支持取 ...

随机推荐

  1. matlab biplot 符号的困惑

    在matlab中做Principal component Analysis 时,常要用biplot 函数来画图,表示原分量与主分量(principal component)之间的关系,以及原始观察数据 ...

  2. Android之——卸载应用程序

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47357729 不多说,不废话,直接上代码,大家都懂得 //卸载应用程序 //參数为 ...

  3. hive job oom问题

    错误信息例如以下:Container [pid=26845,containerID=container_1419056923480_0212_02_000001] is running beyond ...

  4. 自己动手写CPU之第七阶段(2)——简单算术操作指令实现过程

    将陆续上传本人写的新书<自己动手写CPU>.今天是第25篇.我尽量每周四篇 亚马逊的预售地址例如以下,欢迎大家围观呵! http://www.amazon.cn/dp/b00mqkrlg8 ...

  5. mongodb 配置单实例与双实例

    环境: centos6.5 192.168.16.70 配置单实例mongodb:[root@www soft]# tar xf mongodb-linux-x86_64-rhel62-3.2.7.t ...

  6. 【BZOJ3295】[Cqoi2011]动态逆序对 cdq分治

    [BZOJ3295][Cqoi2011]动态逆序对 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依 ...

  7. 基于EasyNVR二次开发实现业务需求:用户、权限、设备管理

    许多接触到EasyNVR的用户.开发者都会提出关于EasyNVR设备分组和账户设备关系映射的问题,我们参考目前大部分的视频能力输出平台的做法,EasyNVR目前只做了唯一的用户/密码(类比appkey ...

  8. python中装饰器你真的理解吗?

    def w1(func): print('装饰器1....') def w1_in(): print('w1_in.....') func() return w1_in def w2(func): p ...

  9. Centos7升级python版本

    升级Python 我的安装目录是/home/python 下载 ` cd /home/python wget https://www.python.org/ftp/python/3.5.0/Pytho ...

  10. python数据分析之Pandas:基本功能介绍

    Pandas有两个主要的数据结构:Series和DataFrame. Series是一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据标签构成.来看下它的使用过程 In [1]: from  ...