WindowsManager.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace NetworkRemoteControlLib.Windows
  4. {
  5. /// <summary>
  6. /// Windows管理类(主要有关机重启等功能,API函数实现)
  7. /// </summary>
  8. public class WindowsManager
  9. {
  10. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  11. internal struct TokPriv1Luid
  12. {
  13. public int Count;
  14. public long Luid;
  15. public int Attr;
  16. }
  17. [DllImport("kernel32.dll", ExactSpelling = true)]
  18. internal static extern IntPtr GetCurrentProcess();
  19. [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  20. internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
  21. [DllImport("advapi32.dll", SetLastError = true)]
  22. internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
  23. [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
  24. internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
  25. ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
  26. [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
  27. internal static extern bool ExitWindowsEx(int flg, int rea);
  28. internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
  29. internal const int TOKEN_QUERY = 0x00000008;
  30. internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
  31. internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
  32. /// <summary>
  33. /// 注销
  34. /// </summary>
  35. internal const int EWX_LOGOFF = 0x00000000;
  36. /// <summary>
  37. /// 关机
  38. /// </summary>
  39. internal const int EWX_SHUTDOWN = 0x00000001;
  40. /// <summary>
  41. /// 重启
  42. /// </summary>
  43. internal const int EWX_REBOOT = 0x00000002;
  44. internal const int EWX_FORCE = 0x00000004;
  45. internal const int EWX_POWEROFF = 0x00000008;
  46. internal const int EWX_FORCEIFHUNG = 0x00000010;
  47. private static void DoExitWin(int flg)
  48. {
  49. bool ok;
  50. TokPriv1Luid tp;
  51. IntPtr hproc = GetCurrentProcess();
  52. IntPtr htok = IntPtr.Zero;
  53. ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
  54. tp.Count = 1;
  55. tp.Luid = 0;
  56. tp.Attr = SE_PRIVILEGE_ENABLED;
  57. ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
  58. ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
  59. ok = ExitWindowsEx(flg, 0);
  60. }
  61. /// <summary>
  62. /// 关机
  63. /// </summary>
  64. public static void ShutDown()
  65. {
  66. // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。
  67. // 在XP下可以看到帮助信息,以得到不同得参数
  68. // SHUTDOWN /?
  69. DoExitWin(EWX_SHUTDOWN);
  70. }
  71. /// <summary>
  72. /// 重启
  73. /// </summary>
  74. public static void Reboot()
  75. {
  76. DoExitWin(EWX_REBOOT);
  77. }
  78. }
  79. }