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

C#
C#:小编详谈ASP.NET和JSP技术
C#:小编详谈StringBuilder
C#:使用CSS的8种技巧
C#:C#开发技巧之将图片存入数据库
C#:C#技术点之利用Image制作小动画
C#:C#开发技巧之如何根据年份判断十二生肖
C#:如何制作自动播放的MP3播放器
c#:C#技术利用鼠标绘图
C#:禁用鼠标左键
C#:如何使用匿名方法
C#:小编教你如何实现特殊形状的窗体
C#:在C#应用程序控制输入法
C#:小编教大家实现堆栈
C#:C#中数组知识点的精华
C#:小编谈C#中TextBox控件的应用技巧
C#:小编教大家设置货币值中使用的小数位数
C#:C#中实现倒计时功能
C#:小编教大家创建一个数字时钟
C#:小编教大家如何向ListView控件添加搜索功能
C#:小编浅谈如何在DataGridView控件中验证数据输入

C#关闭电脑


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 135 ::
收藏到网摘: 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()就可以关闭计算机