CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

我做了一个自动关机程序,但是如果机器启动屏幕保护(需要输入密码的),这个程序就不好用了。请问为什么?我的关机类代码见内!

楼主zhudao92(房礼云)2006-11-07 10:58:59 在 .NET技术 / C# 提问

using   System;  
  using   System.Runtime.InteropServices   ;//C#中对Win32的API函数的互操作是通过命名空间  
                                                                            //“System.Runtime.InteropServices”中的“DllImport”特征类来实现的。  
   
  namespace   PackAPI  
  {  
  ///   <summary>  
  ///   Class1   的摘要说明。  
  ///   </summary>  
  public   class   ExitWindows  
  {  
  [StructLayout(LayoutKind.Sequential,   Pack=1)]//公共语言运行库利用StructLayoutAttribute控制类或结构  
                                                                                            //的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列。  
                                                                                    //LayoutKind分为三种方式排列非托管内存:Auto、Explicit、Sequential  
                                                                                    //当选择用Sequential时需要用Pack规定大小。Pack的值为:   0,   1,   2,   4,   8,   16,   32,   64,   128  
  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   );//打开进程相联系的access   token  
                                                                                                                                                                          //access   token   (通道令牌?)是包含一个会话的安全信息  
   
  [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,//赋予或解除一个access   token的特权  
  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   int   TOKEN_ADJUST_DEFAULT=0x0080;//以下TOKEN的值本例中没有用到  
  internal   const   int   TOKEN_ADJUST_GROUPS=0x0040;  
  internal   const   int   TOKEN_ADJUST_SESSIONID=0x0100;  
  internal   const   int   TOKEN_ASSIGN_PRIMARY=0x0001;  
  internal   const   int   TOKEN_DUPLICATE=0x0002;  
  internal   const   long   TOKEN_EXECUTE=0x20000L;  
  internal   const   int   TOKEN_IMPERSONATE=0x0004;  
  internal   const   int   TOKEN_QUERY_SOURCE=0x0010;  
  //internal   const   int   TOKEN_READ=0x200e0L;  
          internal   const   long   TOKEN_WRITE=0x200e0L;  
  internal   const   long   TOKEN_ALL_ACCESS=0xf00ffL;  
  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;  
  public   ExitWindows()  
  {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
  }  
  public   bool   DoExitWindows(int   how)//执行  
  {  
  bool   ok;  
  TokPriv1Luid   tp;  
  IntPtr   hproc   =   GetCurrentProcess();//IntPtr是平台提供的一个结构体。它用于描述一个指针或句柄  
  IntPtr   htok   =   IntPtr.Zero;//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   );  
  switch(how)  
  {  
  case   0:  
          ok   =   ExitWindowsEx(   EWX_LOGOFF,   0   );  
  break;  
  case   1:  
  ok   =   ExitWindowsEx(   EWX_SHUTDOWN,   0   );  
  break;  
  case   2:  
  ok   =   ExitWindowsEx(   EWX_REBOOT,   0   );  
  break;  
  case   3:  
  ok   =   ExitWindowsEx(   EWX_FORCE,   0   );  
  break;  
  case   4:  
  ok   =   ExitWindowsEx(   EWX_POWEROFF,   0   );  
  break;  
  case   5:  
  ok   =   ExitWindowsEx(   EWX_FORCEIFHUNG,   0   );  
  break;  
  }  
  return   ok;  
  }  
  }  
  }  
  问题点数:100、回复次数:9Top

1 楼bitpolar(独自看天)回复于 2006-11-07 11:05:51 得分 0

学习Top

2 楼oolongTea(顶者)回复于 2006-11-07 11:14:01 得分 0

关注  
  据说C++做的才可以  
  Top

3 楼zhoujianlong()回复于 2006-11-07 11:17:40 得分 0

顶Top

4 楼SysPlus(万诱引力)回复于 2006-11-07 11:24:35 得分 0

Process   proc   =   New   Process();  
  proc.StartInfo.FileName   =   "shutdown";  
  proc.StartInfo.Arguments   =   "-f   -t   0";  
  proc.Start();Top

5 楼csShooter(Sharp Shooter)回复于 2006-11-07 12:00:05 得分 0

用不着吧,又在做流氓软件Top

6 楼yeerh(边城浪)回复于 2006-11-07 12:46:56 得分 0

学习.顶一下Top

7 楼Qim(莫名-从星做起......)回复于 2006-11-07 13:19:53 得分 0

MarkTop

8 楼lovefootball(蟑螂(生活就是扯淡--做人要放低姿态))回复于 2006-11-07 13:23:59 得分 0

http://www.programfan.com/article/showarticle.asp?id=2762Top

9 楼yuanzhihua520(超市系统,短信系统全国找代理qq:694545800)回复于 2007-01-05 14:22:03 得分 0

学习.顶一下Top

相关问题

关键词

得分解答快速导航

  • 帖主:zhudao92

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo