安全关机程序

安全关机程序

最近在实验室用ftp下点东西,但是由于实验室晚上12点就会断电。于是
需要在此之前关掉机器,图省事就用WindowsXP自带的计划任务每次设置
成11:50就调用“shutdown -s”命令自动关机。但是好几次都发现没法
正常关机,第二天早上起来就会检测磁盘。于是就做了个实验,发现确实
当使用flashfxp下载东西时,关机会不能正常关机,等待确定终止flashfxp
程序。

发现原因后,很简单,先把用户进程全部terminate掉,然后再自动关机。
于是就有了本文。

具体内容很简单,用CreateToolhelp32Snapshot函数得到当前进程快照,
然后Process32First和Process32Next函数循环得到进程的ID号。然后再
调用OpenProcess得到进程句柄和其优先级类。从而判断是否是用户进程,
将之Terminate掉(使用TerminateProcess函数)。最后调用ExitWindowsEx
函数关闭机器。

为了防止意外,在程序中还再次调用windows程序:shutdown -s命令来关
闭机器。

具体代码如下:

/*
   AutoShutDown.cpp
*/

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>


BOOL fn_KillAllUserProcess();

BOOL fn_ShutdownComputer();

void error(char * szErrorMessage);


void main( )
{
  fn_KillAllUserProcess();
  fn_ShutdownComputer();
}

BOOL fn_KillAllUserProcess()
{
 HANDLE hProcessSnap;
 HANDLE hProcess;
 PROCESSENTRY32 pe32;
 DWORD  dwPriorityClass;

 
 // Take a snapshot of all processes in the system.
 hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0

);
 if( hProcessSnap == INVALID_HANDLE_VALUE )
 {
  return FALSE;
 }
 
 // Set the size of the structure before using it.
 pe32.dwSize = sizeof( PROCESSENTRY32 );
 
 // Retrieve information about the first process,
 // and exit if unsuccessful
 if( !Process32First( hProcessSnap, &pe32 ) )
 {
  CloseHandle( hProcessSnap );     // Must clean up the

snapshot object!
  return  FALSE;
 }
 
 // Now walk the snapshot of processes, and
 // display information about each process in turn
 do
 {
  // Retrieve the priority class.
  dwPriorityClass = 0;
  hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE,

pe32.th32ProcessID );
  if( hProcess != NULL )
  {
   dwPriorityClass = GetPriorityClass( hProcess );
  }
  
  //judge if the user's process and if the process of

this program
  if (dwPriorityClass == 32 && stricmp(pe32.szExeFile,

"AutoShutDown.exe") != 0)
  {
   //terminate the prcess
   TerminateProcess(hProcess, 0);
  }
  
  if (hProcess != NULL)
   CloseHandle(hProcess);
  
 } while( Process32Next( hProcessSnap, &pe32 ) );
 
 CloseHandle( hProcessSnap );

 return TRUE ;

}

BOOL fn_ShutdownComputer()
{
 HANDLE hToken;
 TOKEN_PRIVILEGES tkp;
 
 // Get a token for this process.
 
 if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  error("OpenProcessToken");
 
 // Get the LUID for the 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 the 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)
  error("AdjustTokenPrivileges");
 
 // Shut down the system and force all applications to close.
 
 if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
  error("ExitWindowsEx");


 //sleep 30 seconds
 Sleep(30000);

 //use the onther way to shundown the computer
 system("shutdown -s");

 return true;
}

void error(char * szErrorMessage)
{
 printf("%s/n", szErrorMessage);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值