问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

C#用API 函数的ExitWindowsEx()方法怎么无法关机

发布网友 发布时间:2024-03-27 10:36

我来回答

5个回答

热心网友 时间:2024-03-27 13:20

public class WindowsExit
{
[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 Reboot()
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
}

public static void PowerOff()
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
}

public static void LogoOff()
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}

热心网友 时间:2024-03-27 13:20

class EnvironmentCheckClass
{

[DllImport("user32.dll")]
static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
[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);

[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

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";

public enum ExitWindows : uint
{
LogOff = 0x00, //注销
ShutDown = 0x01, //关机
Reboot = 0x00000002, //重启
Force = 0x04,
PowerOff = 0x08,
ForceIfHung = 0x10
}
public 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
}

public void ReStartComputer()
{
System.Threading.Thread Restart = new Thread(ReSart);
Restart.Start();
}
private void ReSart()
{
try
{
System.Threading.Thread.Sleep(1000);
//2
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(ExitWindows.Reboot, ShutdownReason.MinorOther);
}
catch
{ }
}

}
修改ok = ExitWindowsEx(ExitWindows.Reboot中的ExitWindows.Reboot 直接可以用!

热心网友 时间:2024-03-27 13:21

去google上搜一下,API很强大……

热心网友 时间:2024-03-27 13:21

好像这个函数有一些参数吧。要写得好的话,帮我们的ppc写个关机程序吧,就是关掉电源的关机

热心网友 时间:2024-03-27 13:22

代码贴出来看看,说的太模糊
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
苹果电脑电池充不进电苹果电脑充不进去电是怎么回事 苹果电脑不充电没反应苹果电脑充电指示灯不亮充不了电怎么办 狗狗更加忠诚护家、善解人意,养一只宠物陪伴自己,泰迪能长多大... 描写泰迪狗的外形和特点的句子 国外留学有用吗 花钱出国留学有用吗 !这叫什么号 百万医疗赔付后是否可以续保 前一年理赔过医疗险还能续保吗? 医疗住院险理赔后还能购买吗? 网易邮箱怎么不能翻译了 为什么有的蚂蚁长着蜜蜂的翅膀? 用蚂蚁蜜蜂和树叶编个故事 货车空调不凉了是什么原因? 信息管理与信息系统这一专业学什么的啊! 郑州有手就会的游戏搬砖项目,求内推? 魔兽世界冬幕节礼物能开出几种宠物,分别是什么?礼物能开几次?1 ...win10专业版×64位系统为什么玩生化危机2重制版打开了过9秒闪退... 螺栓硬度的简介 芯部硬度和表面硬度怎么测量方法 芯部硬度的检测位置 江苏扬州劳动社会保障局电话号码 俄罗斯线缆标准和国标区别 电信黄金速率怎么开通 等离子轨道打滑原因 紫光mp4是什么意思 《影视剧孙大圣宇宙游记》最新txt全集下载 清华紫光VP+897的MP5 我用它下载了部电影 看到三分之一就自动退出了 为... 武安紫光花园房子质量 紫光5.0天空下载哪儿有 高手看看这个CPU是多大的? 在直流电路中电阻在电路中相当于 什么?电容在电路中相当于什么?_百度... 切好的水果能放在杯子里5个小时么,要做火车,想到第二天早上吃 插尿管怎么拉大便,怎么办 老年人插导尿管会引起每天大便次数增多吗 为什么插了导尿管大便次数便多,每次量也不多。每次一两坨,因为要插半... 燃料在燃料电池中(SOFC)为什么不燃烧 交往与通信的区别是什么? 主城圣诞树下的礼品盒什么时候能打开1 奥格圣诞树下的礼物什么时候儿能开呀? 圣诞树下的礼物只能开1次吗? 但好像没叹号呀,别人说是要在平安夜才能开礼物,是不是圣诞树下...1 圣诞为什么要放在圣诞树下礼物5 冬幕节期间过了25号还能在圣诞树下开箱子拿礼物吗? 支教对象有哪些要求? MAC牛血色口红适合日常涂吗 wow圣诞节礼物在哪里领11 wow圣诞节AG的树下的礼物什么时候可以开? 3点水正确书写方式 ...几年内未就业高校毕业生与之签订一年以上劳动合同并为其缴纳_百度知...