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

C#
C#.Net网络程序开发-Socket篇
My Singleton in C#
My Prototype in C#
My FactoryMethod in C#
C# 编码规范和编程好习惯
C#数据库操作的三种经典用法
C#实现24点算法源代码
C#中使用GDI 让网站新闻标题个性化
Java util.concurrent中LockSupport类在C#中的实现
如何使用C#进行Visio二次开发
论C#变得越来越臃肿是不可避免的
C-Sharp开发应避免的几个小滥用
C#实现类似qq的屏幕截图程序
C#关闭电脑
用C#画树
C#从视频截图的方法
把new、virtual、override说透
关于enum应用的总结
C#修饰符总结
c#定位CUP所有问题

C#关闭电脑


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