JCHub

  • Home
  • Category
    • A/V
    • WebRTC
    • Beauty of Programming
    • Linux
    • Windows
    • Moments of Life
    • Campus Life
  • Reference
    • API Reference
    • Utilities
    • AV Test
    • Doc
  • Message Board
  • About
JCHub
Code as My Sword, Lost in Obsession
  1. Main page
  2. Beauty of Programming
  3. Main content

C++实现windows重启

2015年6月13日 2145hotness 4likes 0comments

下面这个函数实现了让Windows系统重启的功能,首先是提升权限,然后主要是函数ExitWindowsEx的使用:

C++实现windows重启
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void xxxxxDlg::RestartPC()
{
HANDLE ToHandle;
TOKEN_PRIVILEGES tkp;
 
//打开本进程访问token
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &ToHandle))
{
//修改本进程权限
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
//通知系统已修改
AdjustTokenPrivileges(ToHandle, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
//获得权限后关闭计算机,要实现注销或重启则对应EWX_LOGOFF,EWX_REBOOT
ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
}
}

ExitWindowsEx函数使用说明参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx

This article is licensed with Creative Commons Attribution-NonCommercial-No Derivatives 4.0 International License
Tag: C++
Last updated:2015年6月24日

Jeff

管理员——代码为剑,如痴如醉

Tip the author Like
< Last article
Next article >

Comments

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
Cancel

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts
  • Nodejs C++ addon开发简介
  • 网络字节转换到本地字节的函数模板
  • MFC自绘带背景颜色标题栏
  • VC++获取本机IP地址
  • FLTK程序编译错误
Categories

COPYRIGHT © 2026 jianchihu.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang