科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道使用C#编写的一个定时关机程序之六

使用C#编写的一个定时关机程序之六

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

   /// /// Exits windows (and tries to enable any required access rights, if necesarry). /// ///

作者:中国IT实验室 来源:中国IT实验室 2007年9月11日

关键字: C# 使用 编程

  • 评论
  • 分享微博
  • 分享邮件
  

  /// <summary>
  /// Exits windows (and tries to enable any required access rights, if necesarry).
  /// </summary>
  /// <param name="how">One of the RestartOptions values that specifies how to exit windows.</param>
  /// <param name="force">True if the exit has to be forced, false otherwise.</param>
  /// <exception cref="PrivilegeException">There was an error while requesting a required privilege.</exception>
  /// <exception cref="PlatformNotSupportedException">The requested exit method is not supported on this platform.</exception>
  public static void ExitWindows(RestartOptions how , bool force ) {
   switch(how) {
    case RestartOptions.Suspend:
     SuspendSystem(false, force);
     break;
    case RestartOptions.Hibernate:
     SuspendSystem(true, force);
     break;
    default:
     ExitWindows((int)how, force);
     break;
   }
  }
  /// <summary>
  /// Exits windows (and tries to enable any required access rights, if necesarry).
  /// </summary>
  /// <param name="how">One of the RestartOptions values that specifies how to exit windows.</param>
  /// <param name="force">True if the exit has to be forced, false otherwise.</param>
  /// <remarks>This method cannot hibernate or suspend the system.</remarks>
  /// <exception cref="PrivilegeException">There was an error while requesting a required privilege.</exception>
  protected static void ExitWindows(int how , bool force ) {
   EnableToken("SeShutdownPrivilege");
   if (force )
    how = how | EWX_FORCE;
   if (ExitWindowsEx(how, 0) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
  }
  /// <summary>
  /// Tries to enable the specified privilege.
  /// </summary>
  /// <param name="privilege">The privilege to enable.</param>
  /// <exception cref="PrivilegeException">There was an error while requesting a required privilege.</exception>
  protected static void EnableToken(string privilege ) {
   if (!CheckEntryPoint("advapi32.dll", "AdjustTokenPrivileges"))
    return;
   IntPtr tokenHandle = IntPtr.Zero;
   LUID privilegeLUID = new LUID();
   TOKEN_PRIVILEGES newPrivileges = new TOKEN_PRIVILEGES();
   TOKEN_PRIVILEGES tokenPrivileges ;
   if (OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
   if (LookupPrivilegeValue("", privilege, ref privilegeLUID) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
   tokenPrivileges.PrivilegeCount = 1;
   tokenPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED;
   tokenPrivileges.Privileges.pLuid = privilegeLUID;
   int size = 4;
   if (AdjustTokenPrivileges(tokenHandle, 0, ref tokenPrivileges, 4 + (12 * tokenPrivileges.PrivilegeCount), ref newPrivileges, ref size) == 0)
    throw new PrivilegeException(FormatError(Marshal.GetLastWin32Error()));
  }
  /// <summary>
  /// Suspends or hibernates the system.
  /// </summary>
  /// <param name="hibernate">True if the system has to hibernate, false if the system has to be suspended.</param>
  /// <param name="force">True if the exit has to be forced, false otherwise.</param>
  /// <exception cref="PlatformNotSupportedException">The requested exit method is not supported on this platform.</exception>
  protected static void SuspendSystem(bool hibernate , bool force  ){
   if (!CheckEntryPoint("powrprof.dll", "SetSuspendState"))
    throw new PlatformNotSupportedException("The SetSuspendState method is not supported on this system!");
   SetSuspendState((int)(hibernate ? 1 : 0), (int)(force ? 1 : 0), 0);
  }
  /// <summary>
  /// Checks whether a specified method exists on the local computer.
  /// </summary>
  /// <param name="library">The library that holds the method.</param>
  /// <param name="method">The entry point of the requested method.</param>
  /// <returns>True if the specified method is present, false otherwise.</returns>
  protected static bool CheckEntryPoint(string library , string method ) {
   IntPtr  libPtr = LoadLibrary(library);
   if (!libPtr.Equals(IntPtr.Zero)) {
    if (!GetProcAddress(libPtr, method).Equals(IntPtr.Zero)) {
     FreeLibrary(libPtr);
     return true;
    }
    FreeLibrary(libPtr);
   }
   return false;
  }
  /// <summary>
  /// Formats an error number into an error message.
  /// </summary>
  /// <param name="number">The error number to convert.</param>
  /// <returns>A string representation of the specified error number.</returns>
  protected static string FormatError(int number ) {
   StringBuilder buffer =new StringBuilder(255);
   FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, number, 0, buffer, buffer.Capacity, 0);
   return buffer.ToString();
  }
}
/// <summary>
/// The exception that is thrown when an error occures when requesting a specific privilege.
/// </summary>
public class PrivilegeException : Exception {
  /// <summary>
  /// Initializes a new instance of the PrivilegeException class.
  /// </summary>
  public PrivilegeException () : base() {}
  /// <summary>
  /// Initializes a new instance of the PrivilegeException class with a specified error message.
  /// </summary>
  /// <param name="message">The message that describes the error.</param>
  public PrivilegeException (string message ) :base(message) {}
}
}

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章