#region 关机代码
//C#关机代码
// 这个结构体将会传递给API。使用StructLayout
//(...特性,确保其中的成员是按顺序排列的,C#编译器不会对其进行调整。 [StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid { public int Count; public long Luid; public int Attr; } // 以下使用DllImport特性导入了所需的Windows API。
// 导入的方法必须是static extern的,并且没有方法体。
//调用这些方法就相当于调用Windows API。 [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); //C#关机代码 // 以下定义了在调用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,正好是关机用的。
//C#关机代码
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);
}
#endregion

c# 自动关机代码的更多相关文章

  1. Android自动关机代码

    http://www.open-open.com/lib/view/open1409209890713.html 要实现自动关机的功能需要手机有root权限,如果手机有root权限,运行该程序时,会提 ...

  2. 电脑设置自动关机和取消自动关机代码bat命令

    一.设置定时关机bat 1.首先在桌面新建一个记事本txt 2.打开记事本,输入“shutdown -s -t 1260 ”1260是指1260秒,即21分钟内关机.可以修改成别的时间. 3.保存后, ...

  3. C#电脑自动关机代码指令

    Process p = new Process();//实例化一个独立进程                p.StartInfo.FileName = "cmd.exe";//进程 ...

  4. python 3.6 urllib库实现天气爬取、邮件定时给妹子发送天气

    #由于每天早上要和妹子说早安,于是做个定时任务,每天早上自动爬取天气,发送天气问好邮件##涉及模块:#(1)定时任务:windows的定时任务#             配置教程链接:http://b ...

  5. c/c++中system函数在Linux和windows下区别

    windows 在windows下的system函数中命令可以不区别大小写! 功 能: 发出一个DOS命令 #include <stdlib.h> int system(char *com ...

  6. windows7自动登录后锁定 & 其他VBS

    首先设置自动登录(原已设置登录密码),在开始菜单搜索框输 入 “netplwiz” 按 回车,打开高级用户控制面板,然后取消对“要使用本机,用户需输入用户名和密码(E)”项的勾选,系统弹出窗口要求输入 ...

  7. 黑客整人代码,vbS整人代码大全(强制自动关机、打开无数计算器、无限循环等)

    vbe与vbs整人代码大全,包括强制自动关机.打开无数计算器.无限循环等vbs整人代码,感兴趣的朋友参考下.vbe与vbs整人代码例子:set s=createobject("wscript ...

  8. SunRay4(新蕾4) 定时自动关机方案, Linux后台自动任务crontab实践

    目录: 需求和思路分析 具体实现步骤 理解Crontab Crontab具体参数详细说明 最近碰到一个想要实现定时自动关机的功能,关机的指令无非就是: shutdown -h time 调用openw ...

  9. Dos代码

    #自动解压apk cd /d %~dp1set filename=%~nx1set new_filename=%~n1.zipcopy %filename% %new_filename%echo %f ...

随机推荐

  1. Django-rest-framework(三)view and viewsets使用

    DRF 中有多种view和viewsets,我整理了一下,如下图所示,接下来,我们分别了解下view,viewsets. APIView 所有的view,viewsets都是继承APIView,而AP ...

  2. TCP三次挥手四次握手

    三次握手: 客户端发起: 1.向服务器端发送报文SYN=1,ACK=0;客户端进入SYN-SEND状态. 2.服务端收到SYN=1,ACK=0的请求报文,向客户端返回确认报文SYN=1,ACK=1,服 ...

  3. Ajax 跨域的几种解决方案

    作者:黄轩链接:http://www.zhihu.com/question/19618769/answer/38934786来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处 ...

  4. python脚本批量杀死redis链接

    #!/usr/bin/python #coding:utf8 import sys import commands,time,re ##自定义redis链接类 from redismodel impo ...

  5. Redis4.0新特性

    redis 4.0 新特性 Redis 4.0在2017年7月发布为GA.包含几个重大改进:更好的复制(PSYNC2),线程DEL / FLUSH,混合RDB + AOF格式,活动内存碎片整理,内存使 ...

  6. PHP中文乱码分类及解决办法大全

    PHP+MYSQL做网站开发通常都会碰到浏览器输出中文字符时乱码,这个问题的原因主要是因为HTML内容编码,PHP文件编码和MySQL数据库编码这三者不一致造成的.下面我们以UTF-8为例简述一下如何 ...

  7. C语言实现计算二进制数字1的个数

    #include<stdio.h> #include<stdlib.h> int print_one_bits01(unsigned int value){ //0000 11 ...

  8. fopen,fwrite,fread使用

    fopen, fwrite, fread详解 1.头文件 #include <stdio.h> 2.fopen (1) 函数原型 FILE *fopen(char *filename, * ...

  9. JAVA学习一 对象数组

    对象数组 今天在写一个代码,才发现自己对于对象数组的理解是不够的,那么就讲讲自己现在的理解. 对于数组中的每一个元素都是一个针对对象的引用 他会指向你的具体的一个堆上的对象,它本身知识一个地址值,与其 ...

  10. linux c fprintf()

    #include<stdio.h> #include<unistd.h> #include<time.h> int main(int argc,char *argv ...