zoukankan      html  css  js  c++  java
  • C# API强制关机、重启以及注销计算机

    在Windows系统中有2种方式进行关机、重启以及注销计算机操作:

    1、使用shutdown()命令;2、使用系统API;

    以下是使用系统API进行操作的实例。

    程序实例界面:

    程序实例代码:

    复制代码
      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Text;
      7 using System.Windows.Forms;
      8 
      9 using System.Runtime.InteropServices;
     10 
     11 
     12 namespace ShutDownSys
     13 {
     14     public partial class Form1 : Form
     15     {
     16         public Form1()
     17         {
     18             InitializeComponent();
     19         }
     20 
     21         public class ShutDownSys
     22         { 
     23             //C#关机代码
     24             //这个结构体将会传递给API。使用StructLayout
     25             //(...特性,确保其中成员是按顺序排列的,C#编译器不会对其进行调整)
     26             [StructLayout(LayoutKind.Sequential, Pack = 1)]
     27             internal struct TokPriv1Luid
     28             {
     29                 public int Count; public long Luid; public int Attr;
     30             }
     31 
     32             //以下使用DLLImport特性导入了所需的Windows API。
     33             //导入这些方法必须是static extern的,并且没有方法体。
     34             //调用这些方法就相当于调用Windows API。
     35             [DllImport("kernel32.dll", ExactSpelling = true)]
     36             internal static extern IntPtr GetCurrentProcess();
     37 
     38             [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
     39             internal static extern bool OpenProcessToken(IntPtr h,int acc,ref IntPtr phtok);
     40 
     41             [DllImport("advapi32.dll", SetLastError = true)]
     42             internal static extern bool LookupPrivilegeValueA
     43             (string host, string name, ref long pluid);
     44 
     45             [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
     46             internal static extern bool
     47             AdjustTokenPrivileges(IntPtr htok,bool disall,ref TokPriv1Luid newst,int len,IntPtr prev,IntPtr relen);
     48 
     49             [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
     50             internal static extern bool ExitWindowsEx(int flg,int rea);
     51 
     52             //C#关机代码
     53             //以下定义了在调用WinAPI时需要的常数。
     54             //这些常数通常可以从Platform SDK的包含文件(头文件)中找到。
     55             public const int SE_PRIVILEGE_ENABLED = 0x00000002;
     56             public const int TOKEN_QUERY = 0x00000008;
     57             public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
     58             public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
     59             public const int EWX_LOGOFF = 0x00000000;
     60             public const int EWX_SHUTDOWN = 0x00000001;
     61             public const int EWX_REBOOT = 0x00000002;
     62             public const int EWX_FORCE = 0x00000004;
     63             public const int EWX_POWEROFF = 0x00000008;
     64             public const int EWX_FORCEIFHUNG = 0x00000010;
     65             // 通过调用WinAPI实现关机,主要代码再最后一行ExitWindowsEx  //这调用了同名的WinAPI,正好是关机用的。
     66 
     67             
     68             public static void DoExitWin(int flg)
     69             {
     70                 bool ok;
     71                 TokPriv1Luid tp;
     72                 IntPtr hproc = GetCurrentProcess();
     73                 IntPtr htok = IntPtr.Zero;
     74                 ok = OpenProcessToken(hproc,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,ref htok);
     75                 tp.Count = 1;
     76                 tp.Luid = 0;
     77                 tp.Attr = SE_PRIVILEGE_ENABLED;
     78                 ok = LookupPrivilegeValueA(null,SE_SHUTDOWN_NAME,ref tp.Luid);
     79                 ok = AdjustTokenPrivileges(htok,false,ref tp,0,IntPtr.Zero,IntPtr.Zero);
     80                 ok = ExitWindowsEx(flg,0);
     81             }
     82         }
     83 
     84         //调用
     85         public void Reboot()
     86         {
     87             ShutDownSys.DoExitWin(ShutDownSys.EWX_FORCE | ShutDownSys.EWX_REBOOT);
     88         }
     89 
     90         public void ShutDown()
     91         {
     92             ShutDownSys.DoExitWin(ShutDownSys.EWX_FORCE | ShutDownSys.EWX_POWEROFF);
     93         }
     94 
     95         public void LogOff()
     96         {
     97             ShutDownSys.DoExitWin(ShutDownSys.EWX_FORCE | ShutDownSys.EWX_LOGOFF);
     98         }
     99 
    100         //关机
    101         private void button1_Click(object sender, EventArgs e)
    102         {
    103             ShutDown();
    104         }
    105 
    106         //重启
    107         private void button2_Click(object sender, EventArgs e)
    108         {
    109             Reboot();
    110         }
    111 
    112         //注销
    113         private void button3_Click(object sender, EventArgs e)
    114         {
    115             LogOff();
    116         }
    117     }
    118 }
    复制代码

    相关:

    1、用System.Runtime.InteropServices服务的DllImport方法引入非托管代码程序集,例如调用系统API,C语言写的方法等等。在这种情况下,声明必须为static
    extern 主要用于声明在外部实现的方法,同时,还可以定义外部程序集别名,使得可以从单个程序集中引用同一组件的不同版本。

     2、

    ExitwindowsEx函数的原型:

    bool ExitwindowsEx(UINT uFlags,DWORD dwReserved);

    函数功能:
    该函数注销当前用户,关闭系统;或者关闭并重新启动系统。此函数发送WM_QUERYENDSESSION消息给应用程序来确定它们是否能被终止。
    参数:
    uFlags;指定关机类型。此参数必须包括下列值之一:EWX_LOGOFF,EWX_POWEROFF,EWX_REBOOT,EWX_SHUTDOWN。还包括EWX_FORCE,EWX_FORCEIFHUNG两个可选值。

    EWX_LOGOFF:关闭所有调用函数ExitWindowsEx的进程的安全环境里运行的进程,然后注销用户。
    EWX_REBOOT:关闭系统并重新启动系统。
    EWX_SHUTDOWN:关闭系统使之能完全关闭电源,所有文件缓冲区都被清洗到磁盘,所有的运行的进程都停止。

  • 相关阅读:
    C#在window服务配置Log4Net.dll
    致于即将逝去的2108年,2019年您好
    关于:未能加载文件或程序集“ICSharpCode.SharpZipLib”或它的某一个依赖项异常的解决方案
    Vs 中关于项目中的某 NuGet 程序包还原失败:找不到“xxx”版本的程序包“xxx”
    Git分布式版本控制器常用命令和使用
    微信公众平台网页登录授权多次重定向跳转,导致code使用多次问题
    Visual Studio高效实用的扩展工具、插件
    关于微信企业付款到零钱X509Certificate2读取证书信息,发布到服务器访问不到的解决方案
    关于ASP.NET MVC 项目在本地vs运行响应时间过长无法访问时,解决方法!
    彻底关闭windows10自动更新解决方案
  • 原文地址:https://www.cnblogs.com/soundcode/p/11572940.html
Copyright © 2011-2022 走看看