随笔 - 382  文章 - 14  评论 - 509 
  • #region API定义
  • [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;
  • #endregion
  • 调用代码:   
  • 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(EWX_POWEROFF, EWX_FORCE);   
  • //要源文件的来下载
  • //http://www.51code.org/attachments/month_0701/u2007119112014.51code.org.rar
  • posted on 2007-07-19 01:11 玉米丰收 阅读(106) 评论(0)  编辑 收藏 网摘

    标题  
    姓名  
    主页
    Email (博主才能看到) 
    验证码 *  看不清,换一张 [登录][注册]
    内容(请不要发表任何与政治相关的内容)  
      登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
    Google站内搜索
    [推荐职位]上海盛大网络招聘架构师



    China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
    近千种 9-95 新二手计算图书火热销售中!
    开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》

    相关文章:

    相关链接: