Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15646
  • 博文数量: 7
  • 博客积分: 280
  • 博客等级: 二等列兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 17:51
文章分类
文章存档

2008年(7)

我的朋友
最近访客

分类: WINDOWS

2008-05-25 17:51:31

MySystemShutdown(LPTSTR lpMsg)
{
   HANDLE hToken; // handle to process token

   TOKEN_PRIVILEGES tkp; // pointer to token structure

 
   BOOL fResult; // system shutdown flag

 
   // Get the current process token handle so we can get shutdown

   // privilege.

 
   if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
      return FALSE;
 
   // Get the LUID for shutdown privilege.

 
   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
 
   tkp.PrivilegeCount = 1; // one privilege to set

   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
 
   // Get shutdown privilege for this process.

 
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
 
   // Cannot test the return value of AdjustTokenPrivileges.

 
   if (GetLastError() != ERROR_SUCCESS)
      return FALSE;
 
   // Display the shutdown dialog box and start the countdown.

 
   fResult = InitiateSystemShutdown(
      NULL, // shut down local computer

      lpMsg, // message for user

      0, // time-out period, in seconds

      FALSE, // ask user to close apps

      FALSE); // reboot after shutdown

 
   if (!fResult)
      return FALSE;
 
   // Disable shutdown privilege.

 
   tkp.Privileges[0].Attributes = 0;
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
 
   return TRUE;
}

阅读(542) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:任务栏和开始菜单透明

给主人留下些什么吧!~~