如何实现关机(不会被拦截)

可爱的小莱 2011-10-08 09:23:46
如题
...全文
167 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
可爱的小莱 2011-10-09
  • 打赏
  • 举报
回复
up+
alpha.5 2011-10-08
  • 打赏
  • 举报
回复
学习..
jackson35296 2011-10-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jackson35296 的回复:]

system("shutdown -s -t -f 0");
一定要加上-f参数,这样会强制终止正在运行的程序,否则windows一直等待程序的退出,一旦有些程序关机时无法响应,就会卡住关机界面
[/Quote]

顺序写错了,应该是system("shutdown -s -f -t 0");
jackson35296 2011-10-08
  • 打赏
  • 举报
回复
system("shutdown -s -t -f 0");
一定要加上-f参数,这样会强制终止正在运行的程序,否则windows一直等待程序的退出,一旦有些程序关机时无法响应,就会卡住关机界面
jiuzhoulh 2011-10-08
  • 打赏
  • 举报
回复
system("shutdown -s -t 0");
江南烟雨梦 2011-10-08
  • 打赏
  • 举报
回复
要获取操作系统的权限...

/*------------------Operate--------------------*/
void Operate()
{
HANDLE hToken;
TOKEN_PRIVILEGES tTknPrlg;

if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tTknPrlg.Privileges[0].Luid);
tTknPrlg.PrivilegeCount = 1;
tTknPrlg.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tTknPrlg, 0, (PTOKEN_PRIVILEGES)NULL, 0);

switch (gSintRunStat)//待机、关机、重启的状态
{
case 0:
{
ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0);
break;
}
case 1:
{
SetSuspendState(true, true, true);
break;
}
case 2:
{
ExitWindowsEx(EWX_REBOOT, 0);
break;
}
default:
break;
}
}
}

这是待机、重启、关机的部分...
可爱的小莱 2011-10-08
  • 打赏
  • 举报
回复
看来只能cmd了
jyh_baoding 2011-10-08
  • 打赏
  • 举报
回复
大家都对
C# 关机程序 收藏 1. using System; 2. using System.Runtime.InteropServices; 3. 4. class shoutdown{ 5. [StructLayout(LayoutKind.Sequential, Pack=1)] 6. internal struct TokPriv1Luid 7. { 8. public int Count; 9. public long Luid; 10. public int Attr; 11. } 12. 13. [DllImport("kernel32.dll", ExactSpelling=true) ] 14. internal static extern IntPtr GetCurrentProcess(); 15. 16. [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ] 17. internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok ); 18. 19. [DllImport("advapi32.dll", SetLastError=true) ] 20. internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid ); 21. 22. [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ] 23. internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall, 24. ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen ); 25. 26. [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ] 27. internal static extern bool ExitWindowsEx( int flg, int rea ); 28. 29. internal const int SE_PRIVILEGE_ENABLED = 0x00000002; 30. internal const int TOKEN_QUERY = 0x00000008; 31. internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; 32. internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; 33. internal const int EWX_LOGOFF = 0x00000000; 34. internal const int EWX_SHUTDOWN = 0x00000001; 35. internal const int EWX_REBOOT = 0x00000002; 36. internal const int EWX_FORCE = 0x00000004; 37. internal const int EWX_POWEROFF = 0x00000008; 38. internal const int EWX_FORCEIFHUNG = 0x00000010; 39. 40. private static void DoExitWin(int flg) 41. { 42. bool ok; 43. TokPriv1Luid tp; 44. IntPtr hproc = GetCurrentProcess(); 45. IntPtr htok = IntPtr.Zero; 46. ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok ); 47. tp.Count = 1; 48. tp.Luid = 0; 49. tp.Attr = SE_PRIVILEGE_ENABLED; 50. ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid ); 51. ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero ); 52. ok = ExitWindowsEx( flg, 0 ); 53. } 54. 55. public static void Main() 56. { 57. Console.WriteLine("正在关机……"); 58. // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。 59. // 在XP下可以看到帮助信息,以得到不同得参数 60. // SHUTDOWN /? 61. DoExitWin(EWX_SHUTDOWN); 62. } 63. } 64. 65.

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧