-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ SOURCES += \ | |
|
||
HEADERS += \ | ||
PolicyConfig.h \ | ||
UAC.h \ | ||
audiomanager.h \ | ||
custom.h \ | ||
lazydog.h \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef UAC_H | ||
#define UAC_H | ||
|
||
#include <ShlObj_core.h> | ||
#include <QCoreApplication> | ||
|
||
// for IsUserAnAdmin() | ||
#pragma comment (lib, "Shell32.lib") | ||
|
||
class UAC | ||
{ | ||
public: | ||
// 以管理员权限启动一个新实例 | ||
// true-启动了新实例 | ||
// false-未启动新实例 | ||
static bool runAsAdmin() | ||
{ | ||
if (IsUserAnAdmin()) | ||
{ | ||
return false; // 当前程序正以管理员权限运行 | ||
} | ||
|
||
QStringList args = QCoreApplication::arguments(); // 获取命令行参数 | ||
if (args.count() < 2 || args[1] != "runas") // 不带参数或参数不为"runas"时,即直接运行 | ||
{ | ||
// 获取应用程序可执行文件的路径 | ||
QString filePath = QCoreApplication::applicationFilePath(); | ||
|
||
// 以管理员权限,执行exe程序 | ||
HINSTANCE ins = ShellExecuteA(nullptr, "runas", filePath.toStdString().c_str(), | ||
"runas", nullptr, SW_SHOWNORMAL); | ||
if (ins > (HINSTANCE)32) | ||
{ | ||
return true; // 程序新实例启动成功 | ||
} | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
#endif // UAC_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters