首先要导入对命名空间

using System.Runtime.InteropServices;

的引用

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

}

private void button2_Click(object sender, EventArgs e)

{

//此代码实现重启功能

DoExitWin(EWX_REBOOT);

}

private void button3_Click(object sender, EventArgs e)

{

//此代码实现注销功能

DoExitWin(EWX_LOGOFF);

}

private void button1_Click_1(object sender, EventArgs e)

{

//此代码实现关机功能

DoExitWin(EWX_SHUTDOWN);

}

 

C#实现注销、重启和关机代码的更多相关文章

  1. C/C++控制Windows关机/注销/重启的正确姿势

    简介 说到代码控制Windows关机/注销/重启的方式,有很多种,最简单的不过就是控制命令行,使用system("pause")函数执行一个shutdown -s -t 0,关机就 ...

  2. WIN7远程桌面重启、关机

    在使用远程桌面访问Win7系统时会发现一个小问题,在xp远程桌面中存在的重启和关机菜单在win7远程桌面中不见了,如图: 这也给我们的使用带来了一些小小的麻烦,但实际上微软依然保留了命令行的方式来实现 ...

  3. Ubuntu16.04 中如何挂载第二块磁盘,挂载成功,但是用reboot和shutdown重启或关机后挂载就没有了的解决办法

    本测试机有4块硬盘,初始意图想做一个磁盘阵列,但是在安装系统的时候不知道引导文件如何选择安装,所以暂时不使用磁盘阵列(后期研究) 检测硬盘能否被识别 root@ranxf:/# fdisk -l Di ...

  4. CentOS7重启和关机

    重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.shutdown -r 2 ...

  5. windows C++实现注销、重启、关机 logoff reboot shutdown

    实现这一功能很简单,主要需要调用一个系统API ExitWindowsEx 功能就是,注销当前用户,关闭系统,或者重新启动系统. 它会发送一个WM_QUERYENDSESSION消息给所有的应用程序, ...

  6. C#程序注销、重启、关机和锁定电脑

    一:截图 二:源代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  7. c++ win32 关机 注销 重启

    #include <iostream> #include <Windows.h> #pragma comment(lib, "user32.lib") #p ...

  8. WPF实现在电脑重启或关机时执行某些逻辑

    Application类的SessionEnding事件,就是电脑关机或重启时响应的(会话结束事件), 所以只需要在App.xaml中添加SessionEnding <Application x ...

  9. 【树莓派】RPi desktop系统重启或关机挂起几个问题:plymouth-reboot.service、plymouth-poweroff.service、Deconfiguring network interfaces

    在基于intel平台安装的RPi desktop关机或者重启时,会存在挂起的问题,一直卡着不动. 挂起问题有3个: 系统关机时候,停留在:plymouth-poweroff.service 系统重启时 ...

随机推荐

  1. why dicePlayer cannot player with defy mb526

    硬件加速视频播放器 DicePlayer v2.0.38 ... ..... ...... ........ \ 局限性:- 视频兼容性依赖于您设备的视频硬解码能力

  2. MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存

    目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...

  3. 《Python基础教程(第二版)》学习笔记 -> 第九章 魔法方法、属性和迭代器

    准备工作 >>> class NewStyle(object): more_code_here >>> class OldStyle: more_code_here ...

  4. Linux操作系统上用数据泵导库

    1.在Linux上 创建物理目录dp_dir,存放数据库.dmp文件: 用root用户登录,切换到oracle用户,用oralce身份创建物理目录如下: [root@server36 oracle]# ...

  5. CSS选择器的兼容性

    CSS 1 CSS2.1 CSS3 :hover 在IE6中只有a元素可用.E:empty 貌似在webkit核心浏览器中有些小bug.如果这个bug依然存在,不太确定如何测试.IE6不支持.clas ...

  6. Tableau学习笔记之四

    创建基本变量图表: 1.可以创建表格,条形图,饼图,直方图,线图,堆积条形图,箱线图等. 2.根据自己选择的变量和维度的数量,Tableau中的“智能显示”会相应的提醒,可以绘制哪些图形,可以绘制的一 ...

  7. motan解读:添加spring 支持

    代码位置: motan-core的目录下       motan中使用spring管理配置对象.motan利用Spring的spi机制创建了自定义标签和相应的标签处理代码.具体使用方法见这篇.本文以m ...

  8. RabbitMQ三种Exchange模式(fanout,direct,topic)的特性 -摘自网络

    RabbitMQ中,所有生产者提交的消息都由Exchange来接受,然后Exchange按照特定的策略转发到Queue进行存储 RabbitMQ提供了四种Exchange:fanout,direct, ...

  9. 制作java可执行程序的方法

    命令行方法: 1. 创建Manifest.txt文件,内容: Main-Class: com.mkyong.awt.AwtExample 2.打包所有的class,包括Manifest.txt文件: ...

  10. 编译arm64错误记录

    响应2月底appstore 64位APP的上线要求,开始编译IOS arm64版本引擎库.编译arm64遇到一些问题,在此记录. 1. 数据类型的错误 __int64 相关,提示error: expe ...