ExitDlg.cpp
资源名称:08.zip [点击查看]
上传用户:ynjin1970
上传日期:2014-10-13
资源大小:6438k
文件大小:3k
源码类别:
中间件编程
开发平台:
Visual C++
- // 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();
- }