安全关机程序[通俗易懂]

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

大家好,又见面了,我是你们的朋友全栈君。

安全关机程序

最近在实验室用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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162969.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号