Windows C + + implements logoff, restart, shutdown logoff reboot shutdown

Source: Internet
Author: User

Implementing this functionality is simple, and it is important to call a system API

ExitWindowsEx

The function is to log off the current user, shut down the system, or reboot the system.

It will send a wm_queryendsession message to all the applications and let them decide whether it can be closed.

Function Prototypes:

    1. <span style="font-size:18px;">BOOL WINAPI ExitWindowsEx(
    2. __in UINT uFlags,
    3. __in DWORD dwReason
    4. );
    5. </span>
The first parameter is the closing type and the second is the reason for the shutdown

The first parameter can be:

Ewx_logoff
0

Ewx_poweroff
0x00000008

Ewx_reboot
0x00000002

Ewx_restartapps
0x00000040

Ewx_shutdown
0x00000001

This parameter can also optionally contain the following two values

Ewx_force
0X00000004 contains this parameter to force the system to shut down, which may cause the application to lose data

Ewx_forceifhung
0x00000010

if the application process still does not respond to wm_queryendsession or wm_endsession messages after the timeout period, then force them off.

return Value:

If successful, returns a value other than 0, failure returns 0

More error messages can be obtained by GetLastError ().

In addition to understanding this function, we should also be clear:

For Windows NT versions of the operating system, we need to elevate a se_shutdown permission to complete the shutdown operation.

NT is not required, for example 95,98,me

The above NT systems include:

Microsoft Windows $ (Windows NT 5.0) (1999) (2000-2010) Microsoft Windows XP (Windows NT 5.1) (2001-2014) Microsoft Win Dows Server 2003 (Windows NT 5.2) (2003-2015) Microsoft Windows Server 2003 R2 (Windows NT 5.2) (2006-2015) Microsoft Wind OWS Vista (Windows NT 6.0) (2006-2017) Microsoft Windows Server (Windows NT 6.0) (2008-2018) Microsoft Windows 7 (Win dows NT 6.1) (2009-2020)

Let's start with a concrete implementation:

Shutdown function Implementation

1. Check the system version to see if it is NT above, if yes, to elevate permissions

2. Call the system api,exitwindowsex.

  1. #pragma region 功能实现
  2. BOOL ReSetWindows(DWORD dwFlags,BOOL bForce)
  3. {
  4. //检查参数是否正确
  5. if(dwFlags!=EWX_LOGOFF&&dwFlags!=EWX_REBOOT&&dwFlags!=EWX_SHUTDOWN)
  6. return FALSE;
  7. //获得系统的版本信息,让我们后面确定是否需要提升系统权限
  8. OSVERSIONINFO osvi={0};
  9. //获得参数的大小,以提供给GetVersionEx来判断这是一个新版本的OSVERSIONINFO,还是旧版本的
  10. //新版本是OSVERSIONINFOEX。扩充版本
  11. osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
  12. if(!GetVersionEx(&osvi))
  13. {
  14. return FALSE;
  15. }
  16. //检查操作系统的版本,如果是NT类型的系统,需要提升系统权限
  17. if(osvi.dwPlatformId==VER_PLATFORM_WIN32_NT)
  18. {
  19. //EnableShutDownPriv();
  20. }
  21. //判断是否是强制关机,强制关闭系统进程。
  22. dwFlags|=(bForce!=FALSE)?EWX_FORCE:EWX_FORCEIFHUNG;
  23. //调用API
  24. returnExitWindowsEx(dwFlags,0);
  25. }
code to elevate permissions:
  1. #pragma region 用来提升系统权限
  2. //这是一个通用的提升权限函数,如果需要提升其他权限
  3. //更改LookupPrivilegeValue的第二个参数SE_SHUTDOWN_NAME,即可
  4. BOOL EnableShutDownPriv()
  5. {
  6. HANDLE hToken=NULL;
  7. TOKEN_PRIVILEGES tkp={0};
  8. //打开当前程序的权限令牌
  9. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
  10. {
  11. return FALSE;
  12. }
  13. //获得某一特定权限的权限标识LUID,保存在tkp中
  14. if(!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid))
  15. {
  16. CloseHandle(hToken);
  17. return FALSE;
  18. }
  19. tkp.PrivilegeCount=1;
  20. tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
  21. //调用AdjustTokenPrivileges来提升我们需要的系统权限
  22. if(!AdjustTokenPrivileges(hToken,FALSE,&tkp,sizeof(TOKEN_PRIVILEGES),NULL,NULL))
  23. {
  24. CloseHandle(hToken);
  25. return FALSE;
  26. }
  27. return TRUE;
  28. }
Call Method:
    1. ReSetWindows(EWX_LOGOFF,false);//注销
    2. ReSetWindows(EWX_REBOOT,true);//重启
    3. ReSetWindows(EWX_SHUTDOWN,true);//关机
See your code for details, see attachment. Article excerpt from:http://blog.csdn.net/zy_dreamer/article/details/8948880



Null



List of attachments

Windows C + + implements logoff, restart, shutdown logoff reboot shutdown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.