⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shutdownpc.cpp

📁 VisualC高级编程技术精粹.rar
💻 CPP
字号:
// ShutDownPc.cpp: implementation of the CShutDownPc class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TurnOffPcDll.h"
#include "ShutDownPc.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CShutDownPc::CShutDownPc()
{

}

CShutDownPc::~CShutDownPc()
{

}

void CShutDownPc::ShutDownComputer()
{
	TOKEN_PRIVILEGES tkp;
	HANDLE hToken;
    
    if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
	{
	    MessageBox(NULL,"OpenProcessToken failed!","Error information",MB_ICONERROR);
	}

    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //获得本地机唯一的标识
	tkp.PrivilegeCount = 1;  
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限
    
	if (GetLastError() != ERROR_SUCCESS) 
	{
		MessageBox(NULL,"AdjustTokenPrivileges enable failed!","Error information",MB_ICONERROR);
	}

    int fResult = InitiateSystemShutdown( 
        NULL,                 // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
        //"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!",  // 显示的消息
		"王维玉提示您:系统即将关机,请做好保存工作!",  // 显示的消息
        2,                                // 关机所需的时间
        TRUE,                                 
        FALSE);                             //设为TRUE为重起,设为FALSE为关机

    if(!fResult) 
    { 
         MessageBox(NULL,"InitiateSystemShutdown failed.","Error information",MB_ICONERROR); 
	} 

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

	if (GetLastError() != ERROR_SUCCESS) 
	{
        MessageBox(NULL,"AdjustTokenPrivileges disable failed.","Error information",MB_ICONERROR); 
	} 

   	ExitWindowsEx(EWX_SHUTDOWN,0);     //开始关机	
}

void CShutDownPc::RestartComputer()
{
	TOKEN_PRIVILEGES tkp;
	HANDLE hToken;
    
    if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) 
	{
	    MessageBox(NULL,"OpenProcessToken failed!","Error information",MB_ICONERROR);
	}

    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //获得本地机唯一的标识
	tkp.PrivilegeCount = 1;  
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; 
    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限
    
	if (GetLastError() != ERROR_SUCCESS) 
	{
        MessageBox(NULL,"AdjustTokenPrivileges enable failed!","Error information",MB_ICONERROR);
	}

    int fResult = InitiateSystemShutdown( 
        NULL,                 // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
        //"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!",  // 显示的消息
	     "王维玉提示您:系统即将重启,请做好保存工作!",  // 显示的消息
        2,                                // 关机所需的时间
        TRUE,                                 
        TRUE);                             //设为TRUE为重起,设为FALSE为关机

    if(!fResult) 
    { 
         MessageBox(NULL,"InitiateSystemShutdown failed.","Error information",MB_ICONERROR); 
	} 

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

	if (GetLastError() != ERROR_SUCCESS) 
	{
        MessageBox(NULL,"AdjustTokenPrivileges disable failed.","Error information",MB_ICONERROR); 
	} 

   	ExitWindowsEx(EWX_SHUTDOWN,0);     //开始关机
}

void CShutDownPc::LogOffComputer()
{
	DWORD dwReserved = 0;
	ExitWindowsEx( EWX_FORCE,dwReserved );
}

void CShutDownPc::LockComputer()
{
	WinExec( "C:\\WINDOWS\\system32\\rundll32.exe user32.dll,LockWorkStation", SW_NORMAL );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -