当前位置: 首页 > 图文教程 > .Net技术 > C# > C#关闭电脑

C#
C#和Java的区别
提高C#编程水平的50个要诀
GridView 删除/更新/取消
c#线程
C#泛型有什么好处
总体了解C#
C#2.0匿名函数
GridView中添加一个CheckBox列
C#2.0介绍之Iterators(迭代器)
.NET与Java间进行Web Service交互的选择
C# 2010命名和可选参数的新特性
利用C#远程存取Access数据库
C#中foreach基础使用方法
C#中用鼠标移动页面功能的实现
C# 4.0中泛型协变性和逆变性详解
C#:C# .Net中的类型相互转换教程
C#:C#中的基元类型
C#:语言中的重要知识详细介绍与解释
C#:浅谈C#中的集合对象(Collections)
C#:C#发起邮件会议

C#关闭电脑


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 136 ::
收藏到网摘: n/a

using System;
using System.Runtime.InteropServices;
namespace flyingzl
{
 public class CloseWindow
 {
  [StructLayout(LayoutKind.Sequential, Pack = 1)]
          internal struct TokPriv1Luid
          {
              public int Count;
              public long Luid;
              public int Attr;
          }
         
          [DllImport("kernel32.dll", ExactSpelling = true)]
          internal static extern IntPtr GetCurrentProcess();

          [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
          internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);

          [DllImport("advapi32.dll", SetLastError = true)]
          internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);

          [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
          internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
          ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

          [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
          internal static extern bool ExitWindowsEx(int flg, int rea);

          // 以下定义了在调用WinAPI时需要的常数。这些常数通常可以从Platform SDK的包含文件(头文件)中找到

          internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
          internal const int TOKEN_QUERY = 0x00000008;
          internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
          internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
          internal const int EWX_LOGOFF = 0x00000000;
          internal const int EWX_SHUTDOWN = 0x00000001;
          internal const int EWX_REBOOT = 0x00000002;
          internal const int EWX_FORCE = 0x00000004;
          internal const int EWX_POWEROFF = 0x00000008;
          internal const int EWX_FORCEIFHUNG = 0x00000010;

          // 通过调用WinAPI实现关机,主要代码再最后一行ExitWindowsEx,这调用了同名的WinAPI,正好是关机用的。

          private static void DoExitWin(int flg)
          {
              bool ok;
              TokPriv1Luid tp;
              IntPtr hproc = GetCurrentProcess();
              IntPtr htok = IntPtr.Zero;
              ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
              tp.Count = 1;
              tp.Luid = 0;
              tp.Attr = SE_PRIVILEGE_ENABLED;
              ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
              ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
              ok = ExitWindowsEx(flg, 0);
          }
         
          public static void ShutDown(){
           DoExitWin(EWX_FORCE|EWX_SHUTDOWN);
          }
 }
}

 

以上代码来自网络,经过轻微修改,特此表示感谢!

 调用flyingzl. CloseWindow.ShutDown()就可以关闭计算机