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

📄 exitdlg.cpp

📁 这是书上的代码
💻 CPP
字号:
// ExitDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "ExitDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CExitDlg dialog


CExitDlg::CExitDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CExitDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExitDlg)
	m_bShutDown = FALSE;
	//}}AFX_DATA_INIT
}


void CExitDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExitDlg)
	DDX_Check(pDX, IDC_SHUTDOWN, m_bShutDown);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CExitDlg, CDialog)
	//{{AFX_MSG_MAP(CExitDlg)
	ON_BN_CLICKED(IDC_OK, OnClickedOk)
	ON_BN_CLICKED(IDC_CANCEL, OnClickedCancel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExitDlg message handlers

void CExitDlg::OnOK() 
{
	// TODO: Add extra validation here
}

void CExitDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
}

void CExitDlg::OnClickedOk() 
{
	// TODO: Add your control notification handler code here

	UpdateData(TRUE);
	
	if(m_bShutDown)
	{
	   //  此处所调用的函数大多为API函数;

	   HANDLE processHandle =  NULL;
	   DWORD thisProcessID = 0;
	   thisProcessID = GetCurrentProcessId();     //  得到当前进程的ID;
	   if(!thisProcessID)
          return;
       processHandle = OpenProcess(PROCESS_ALL_ACCESS|STANDARD_RIGHTS_REQUIRED,FALSE,thisProcessID);  //  得到当前进程的句柄;
       if(!processHandle)
	      return;

       HANDLE tokenHandle = NULL;
	   OpenProcessToken(processHandle,TOKEN_ADJUST_PRIVILEGES,&tokenHandle);  //  得到accessToken的句柄;
 	   if(!tokenHandle)
	      return;
	
       CloseHandle(processHandle);  //  进程句柄使用结束后关闭;

       TOKEN_PRIVILEGES tp; 
       LUID luid;
       if(!LookupPrivilegeValue(NULL,"SeShutdownPrivilege",&luid))   //  给luid赋制值;
          return;

	   tp.PrivilegeCount = 1;
       tp.Privileges[0].Luid = luid;
       tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

       AdjustTokenPrivileges(tokenHandle,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),(PTOKEN_PRIVILEGES)NULL,(PDWORD)NULL); 
       if(GetLastError() != ERROR_SUCCESS)
          return; 
  
       if(!ExitWindowsEx(EWX_POWEROFF,0))
          return;
	}
	
	CDialog::OnOK();
}

void CExitDlg::OnClickedCancel() 
{
	// TODO: Add your control notification handler code here
	
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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