a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#調用windows api關機(關機api)示例代碼分享

2020-01-24 02:56:50
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

using System;
using System.Runtime.InteropServices;

class shoutdown{

  [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 );
  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;

  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 Main()
  {
  Console.WriteLine("正在關閉計算機……");
  // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等實現不同得功能。
  // 在XP下可以看到幫助信息,以得到不同得參數
  // SHUTDOWN /?
  DoExitWin(EWX_SHUTDOWN);
  }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久精品1 | 欧美日性 | 最新的黄色网址 | 老牛影视av一区二区在线观看 | 黄色av网站在线免费观看 | 久久91| 超碰人人精品 | 欧美成人一 | 正在播放一区 | 成人精品一区二区三区 | 国产亚洲精品久久 | 亚洲综合一区二区三区 | 特级淫片裸体免费看 | 四虎永久在线 | 激情com| 免费特级黄毛片 | 欧美成人一区二区三区片免费 | 国产亚洲综合一区二区 | 欧美日韩视频在线 | 国产一级视频 | 国产免费一区二区三区最新不卡 | 中文字幕亚洲欧美日韩在线不卡 | 欧美自拍视频 | 国产精品久久久久久久久久99 | 一本色道精品久久一区二区三区 | 久久三区 | 久久极品 | 日韩大片免费播放 | 亚洲国产成人久久一区二区三区 | 日韩成人一区二区 | 国产精品欧美久久久久一区二区 | 亚洲精品日韩激情欧美 | 久久99国产精一区二区三区 | 久久免费国产 | 狠狠草视频 | 欧美久久久久久 | 欧美乱码精品一区二区三 | 一区二区不卡 | 久草视频在线播放 | 国产精品夜色一区二区三区 | 国产成人精品免费视频 |