欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#调用api控制windows关机示例(可以重启/注销)

程序员文章站 2024-02-18 22:40:28
方法一:调用windows自带的shutdown.exe (缺点:会出现倒计时窗口) system.diagnostics.process.start("shutdown...

方法一:调用windows自带的shutdown.exe (缺点:会出现倒计时窗口)

system.diagnostics.process.start("shutdown.exe", "-r -f -t 15");

shutdown参数含义:-r关闭并重启动此计算机;-f 强制运行的应用程序关闭而没有警告;-t 15 延时15shutdown.exe的详细用法:
shutdown [-i | -l | -s | -r | -a] [-f] [-m //computername] [-t xx] [-c "comment"] [-d up:xx:yy]
没有参数 显示此消息(与 ? 相同)
-i 显示 gui 界面,必须是第一个选项
-l 注销(不能与选项 -m 一起使用)
-s 关闭此计算机
-r 关闭并重启动此计算机
-a 放弃系统关机
-m //computername 远程计算机关机/重启动/放弃
-t xx 设置关闭的超时为 xx 秒
-c "comment" 关闭注释(最大 127 个字符)
-f 强制运行的应用程序关闭而没有警告
-d [u][p]:xx:yy 关闭原因代码
u 是用户代码
p 是一个计划的关闭代码
xx 是一个主要原因代码(小于 256 的正整数)
yy 是一个次要原因代码(小于 65536 的正整数)

方法二:调用api

复制代码 代码如下:

private const int se_privilege_enabled = 0x00000002;
private const int token_query = 0x00000008;
private const int token_adjust_privileges = 0x00000020;
private const string se_shutdown_name = "seshutdownprivilege";

[flags]
public enum exitwindows : uint
{
logoff = 0x00, //注销
shutdown = 0x01, //关机
reboot = 0x02, //重启
force = 0x04,
poweroff = 0x08,
forceifhung = 0x10
}

[flags]
private enum shutdownreason : uint
{
majorapplication = 0x00040000,
majorhardware = 0x00010000,
majorlegacyapi = 0x00070000,
majoroperatingsystem = 0x00020000,
majorother = 0x00000000,
majorpower = 0x00060000,
majorsoftware = 0x00030000,
majorsystem = 0x00050000,
minorbluescreen = 0x0000000f,
minorcordunplugged = 0x0000000b,
minordisk = 0x00000007,
minorenvironment = 0x0000000c,
minorhardwaredriver = 0x0000000d,
minorhotfix = 0x00000011,
minorhung = 0x00000005,
minorinstallation = 0x00000002,
minormaintenance = 0x00000001,
minormmc = 0x00000019,
minornetworkconnectivity = 0x00000014,
minornetworkcard = 0x00000009,
minorother = 0x00000000,
minorotherdriver = 0x0000000e,
minorpowersupply = 0x0000000a,
minorprocessor = 0x00000008,
minorreconfig = 0x00000004,
minorsecurity = 0x00000013,
minorsecurityfix = 0x00000012,
minorsecurityfixuninstall = 0x00000018,
minorservicepack = 0x00000010,
minorservicepackuninstall = 0x00000016,
minortermsrv = 0x00000020,
minorunstable = 0x00000006,
minorupgrade = 0x00000003,
minorwmi = 0x00000015,
flaguserdefined = 0x40000000,
flagplanned = 0x80000000
}

[structlayout(layoutkind.sequential, pack = 1)]
private struct tokpriv1luid
{
public int count;
public long luid;
public int attr;
}

[dllimport("kernel32.dll", exactspelling = true)]
private static extern intptr getcurrentprocess();

[dllimport("advapi32.dll", exactspelling = true, setlasterror = true)]
private static extern bool openprocesstoken(intptr h, int acc, ref intptr phtok);

[dllimport("advapi32.dll", setlasterror = true)]
private static extern bool lookupprivilegevalue(string host, string name, ref long pluid);

[dllimport("advapi32.dll", exactspelling = true, setlasterror = true)]
private static extern bool adjusttokenprivileges(intptr htok, bool disall, ref tokpriv1luid newst, int len, intptr prev, intptr relen);

[dllimport("user32.dll")]
private static extern bool exitwindowsex(exitwindows uflags, shutdownreason dwreason);

/// <summary>
/// 关机、重启、注销windows
/// </summary>
/// <param name="flag"></param>
public static void doexitwindows(exitwindows flag)
{
tokpriv1luid tp;
intptr hproc = getcurrentprocess();
intptr htok = intptr.zero;
openprocesstoken(hproc, token_adjust_privileges | token_query, ref htok);
tp.count = 1;
tp.luid = 0;
tp.attr = se_privilege_enabled;
lookupprivilegevalue(null, se_shutdown_name, ref tp.luid);
adjusttokenprivileges(htok, false, ref tp, 0, intptr.zero, intptr.zero);
exitwindowsex(flag, shutdownreason.majorother);
}